In [2]:
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 [3]:
goals = goals[0]['data']['rows']
goals


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

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

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


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

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


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

In [8]:
dictionaryKey1 = []

# For each key in the dictionary's keys,
for key in sessions_dict.keys():
    # add the key to dictionaryKeys
    dictionaryKey1.append(key)

# View the dictionaryKeys list
dictionaryKey1


Out[8]:
['/',
 '/TMRW_Byte_Cafe.php',
 '/TMRW_FAQs.php',
 '/TMRW_the_team.php',
 '/trainstrikes.php',
 '/voteforbyte.php']

In [9]:
dictionaryKey2 = []

# For each key in the dictionary's keys,
for key in goals_dict.keys():
    # add the key to dictionaryKeys
    dictionaryKey2.append(key)

# View the dictionaryKeys list
dictionaryKey2


Out[9]:
['(entrance)', '/', '/TMRW_Byte_Cafe.php', '/TMRW_FAQs.php']

In [12]:
range(len(dictionaryKey2)-1)


Out[12]:
range(0, 3)

In [18]:
dictValues = []

# For each key in the dict's Values,
for x in sessions_dict.values():
    # add the key to dictValues
    dictValues.append(x)

# View the dictionaryValues list
dictValues


Out[18]:
[5925, 270, 129, 140, 231, 79]

In [67]:
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)
    
    added = goals_dict_keys - sessions_dict_keys
    
    removed = sessions_dict_keys - goals_dict_keys
    
    same = set(o for o in intersect_keys if goals_dict[o] == sessions_dict[o])
    
    return intersect_keys


correct_pages = dict_compare(goals_dict, sessions_dict)
#correct_pages = ['/', '/TMRW_Byte_Cafe.php', '/TMRW_FAQs.php']
correct_pages


Out[67]:
{'/', '/TMRW_Byte_Cafe.php', '/TMRW_FAQs.php'}

In [68]:
for page in correct_pages:
    #rint (page)
    for page2 in goals_dict:
        if page == page2:
            print(page2)


/TMRW_Byte_Cafe.php
/TMRW_FAQs.php
/

In [69]:
conv_rate_dict= {}

for page in correct_pages:
    #rint (page)
    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 [46]:
goals_dict[page][1]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-d752c2f99bb6> in <module>()
----> 1 goals_dict[page][1]

TypeError: 'int' object is not subscriptable

In [ ]:
for ghfh in fh:
    if

In [ ]:


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


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-91503d4c5ef6> in <module>()
----> 1 max_value = max(dictValues)
      2 max_value = round(max_value,2)
      3 max_value

ValueError: max() arg is an empty sequence

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


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-2ce6806e3f94> in <module>()
----> 1 min_value = min(dictValues)
      2 min_value = round(min_value,2)
      3 min_value

ValueError: min() arg is an empty sequence

In [11]:
max_page = max(conv_rate_dict, key=conv_rate_dict.get)

if max_page == '/':
    max_page = "homepage"
else:
    max_page = max_page

max_page


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-7b07d793a42c> in <module>()
----> 1 max_page = max(conv_rate_dict, key=conv_rate_dict.get)
      2 
      3 if max_page == '/':
      4     max_page = "homepage"
      5 else:

ValueError: max() arg is an empty sequence

In [12]:
min_page = min(conv_rate_dict, key=conv_rate_dict.get)

if min_page == '/':
    min_page = "homepage"
else:
    min_page = min_page

min_page


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-95941fb7422f> in <module>()
----> 1 min_page = min(conv_rate_dict, key=conv_rate_dict.get)
      2 
      3 if min_page == '/':
      4     min_page = "homepage"
      5 else:

ValueError: min() arg is an empty sequence

In [13]:
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 [14]:
print('Page:"%s'% min_page + '" has the lowest Conversion Rate - %s' % min_value + '% .' )


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

In [ ]:


In [ ]: