In [41]:
# Time period 1st Jan - 30th April (arbitrary )
# API credentials
# Email address 705762800217-compute@developer.gserviceaccount.com
# Key IDs 948ee8e2a420ef14a5d5a29bd35104fe2f1e6ed4
In [42]:
#File for goals (goals_dict)
#Account: TMRW Tech Hub
#Property: TMRW
#View: All Web Site Data
#ids: ga:123303369
#start-date: 2017-02-01
#end-date: 2017-04-30
#metrics
#ga:sessions
#dimensions
#ga:pagePath
#filter
#ga:sessions>30
In [43]:
#File for sessions (sessions_dict)
#Account: TMRW Tech Hub
#Property: TMRW
#View: All Web Site Data
#ids: ga:123303369
#start-date: 2017-02-01
#end-date: 2017-04-30
#metrics
#ga:goal1Completions
#dimensions
#ga:goalCompletionLocation
#filter
#ga:goal1Completions>5
In [1]:
import json
from pprint import pprint
with open('files/TMRW_goal1_goalloc.json') as file:
goals = json.load(file)
goals = goals["reports"][0]['data']['rows']
pprint(goals)
In [3]:
def create_dict(x):
goals_dict = {}
for s in x:
#print (str((s['dimensions'])))
#print (s['metrics'][0]['values'])
#session_dict[] = 0
goals_dict[s['dimensions'][0]] = int(s['metrics'][0]['values'][0])
#session_dict[s['dimensions']] = s['metrics'][0]['values']
return goals_dict
goals_dict = (create_dict(goals))
goals_dict
Out[3]:
In [4]:
with open('files/TMRW_sess_page.json') as file:
sessions = json.load(file)
sessions = sessions["reports"][0]['data']['rows']
pprint(sessions)
In [5]:
def create_dict(x):
sessions_dict = {}
for s in x:
#print (str((s['dimensions'])))
#print (s['metrics'][0]['values'])
#session_dict[] = 0
sessions_dict[s['dimensions'][0]] = int(s['metrics'][0]['values'][0])
#session_dict[s['dimensions']] = s['metrics'][0]['values']
return sessions_dict
sessions_dict = (create_dict(sessions))
sessions_dict
Out[5]:
In [6]:
def dict_compare(goals_dict, sessions_dict):
goals_dict_keys = set(goals_dict.keys())
sessions_dict_keys = set(sessions_dict.keys())
intersect_keys = goals_dict_keys.intersection(sessions_dict_keys)
return intersect_keys
correct_pages = dict_compare(goals_dict, sessions_dict)
correct_pages
Out[6]:
In [7]:
goals_dict_keys = set(goals_dict.keys())
goals_dict_keys
Out[7]:
In [8]:
conv_rate_dict= {}
for page in correct_pages:
for page2 in goals_dict:
if page == page2:
#print(page2)
conv_rate = (goals_dict[page]/(sessions_dict[page])*100)
conv_rate_dict[page] = conv_rate
print(conv_rate_dict)
In [9]:
dictValues = []
# For each key in the dict's Values,
for x in conv_rate_dict.values():
# add the key to dictValues
dictValues.append(x)
# View the dictionaryValues list
dictValues
Out[9]:
In [10]:
max_value = max(dictValues)
max_value = round(max_value,2)
max_value
Out[10]:
In [11]:
min_value = min(dictValues)
min_value = round(min_value,2)
min_value
Out[11]:
In [12]:
max_page = max(conv_rate_dict, key=conv_rate_dict.get)
if max_page == '/':
max_page = "homepage"
else:
max_page = max_page
max_page
Out[12]:
In [13]:
min_page = min(conv_rate_dict, key=conv_rate_dict.get)
if min_page == '/':
min_page = "homepage"
else:
min_page = min_page
min_page
Out[13]:
In [14]:
print('Page:"%s' % max_page + '" has the highest Conversion Rate - %s' % max_value + '% .' )
In [15]:
print('Page:"%s'% min_page + '" has the lowest Conversion Rate - %s' % min_value + '% .' )
In [ ]:
In [ ]: