In [65]:
import json
from pprint import pprint

with open('data/TMRW_goal1_goalloc_copy.json') as file: 
    goals = json.load(file)

goals = goals["reports"]
pprint(goals)


[{'columnHeader': {'dimensions': ['ga:goalCompletionLocation'],
                   'metricHeader': {'metricHeaderEntries': [{'name': 'ga:goal1Completions',
                                                             'type': 'INTEGER'}]}},
  'data': {'isDataGolden': True,
           'maximums': [{'values': ['85']}],
           'minimums': [{'values': ['6']}],
           'rowCount': 4,
           'rows': [{'dimensions': ['(entrance)'],
                     'metrics': [{'values': ['6']}]},
                    {'dimensions': ['/'], 'metrics': [{'values': ['85']}]},
                    {'dimensions': ['/TMRW_Byte_Cafe.php'],
                     'metrics': [{'values': ['22']}]},
                    {'dimensions': ['/TMRW_FAQs.php'],
                     'metrics': [{'values': ['24']}]}],
           'totals': [{'values': ['137']}]}}]

In [66]:
goals = goals[0]['data']['rows']
goals


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

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

sessions = sessions["reports"]
pprint(sessions)


[{'columnHeader': {'dimensions': ['ga:pagePath'],
                   'metricHeader': {'metricHeaderEntries': [{'name': 'ga:sessions',
                                                             'type': 'INTEGER'}]}},
  'data': {'isDataGolden': True,
           'maximums': [{'values': ['5925']}],
           'minimums': [{'values': ['79']}],
           'rowCount': 6,
           'rows': [{'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']}]}],
           'totals': [{'values': ['6774']}]}}]

In [69]:
sessions = sessions[0]['data']['rows']
sessions


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

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

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


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

In [73]:
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_Byte_Cafe.php': 8.148148148148149, '/TMRW_FAQs.php': 18.6046511627907, '/': 1.4345991561181435}

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

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


Out[75]:
1.43

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

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

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


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

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


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

In [ ]:


In [ ]: