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)


[{'dimensions': ['(entrance)'], 'metrics': [{'values': ['6']}]},
 {'dimensions': ['/'], 'metrics': [{'values': ['85']}]},
 {'dimensions': ['/TMRW_Byte_Cafe.php'], 'metrics': [{'values': ['22']}]},
 {'dimensions': ['/TMRW_FAQs.php'], 'metrics': [{'values': ['24']}]}]

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]:
{'(entrance)': 6, '/': 85, '/TMRW_Byte_Cafe.php': 22, '/TMRW_FAQs.php': 24}

In [4]:
with open('files/TMRW_sess_page.json') as file: 
    sessions = json.load(file)

sessions = sessions["reports"][0]['data']['rows']
pprint(sessions)


[{'dimensions': ['/'], 'metrics': [{'values': ['5925']}]},
 {'dimensions': ['/TMRW_Byte_Cafe.php'], 'metrics': [{'values': ['270']}]},
 {'dimensions': ['/TMRW_FAQs.php'], 'metrics': [{'values': ['129']}]},
 {'dimensions': ['/TMRW_the_team.php'], 'metrics': [{'values': ['140']}]},
 {'dimensions': ['/trainstrikes.php'], 'metrics': [{'values': ['231']}]},
 {'dimensions': ['/voteforbyte.php'], 'metrics': [{'values': ['79']}]}]

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]:
{'/': 5925,
 '/TMRW_Byte_Cafe.php': 270,
 '/TMRW_FAQs.php': 129,
 '/TMRW_the_team.php': 140,
 '/trainstrikes.php': 231,
 '/voteforbyte.php': 79}

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]:
{'/', '/TMRW_Byte_Cafe.php', '/TMRW_FAQs.php'}

In [7]:
goals_dict_keys = set(goals_dict.keys())
goals_dict_keys


Out[7]:
{'(entrance)', '/', '/TMRW_Byte_Cafe.php', '/TMRW_FAQs.php'}

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)


{'/TMRW_FAQs.php': 18.6046511627907, '/TMRW_Byte_Cafe.php': 8.148148148148149, '/': 1.4345991561181435}

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]:
[18.6046511627907, 8.148148148148149, 1.4345991561181435]

In [10]:
max_value = max(dictValues)
max_value = round(max_value,2)
max_value


Out[10]:
18.6

In [11]:
min_value = min(dictValues)
min_value = round(min_value,2)
min_value


Out[11]:
1.43

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]:
'/TMRW_FAQs.php'

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]:
'homepage'

In [14]:
print('Page:"%s' % max_page + '" has the highest Conversion Rate - %s' % max_value + '% .' )


Page:"/TMRW_FAQs.php" has the highest Conversion Rate - 18.6% .

In [15]:
print('Page:"%s'% min_page + '" has the lowest Conversion Rate - %s' % min_value + '% .' )


Page:"homepage" has the lowest Conversion Rate - 1.43% .

In [ ]:


In [ ]: