In [105]:
import json
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from pprint import pprint
In [106]:
# in open() you should type your own path of these files(Goal1CompletionLocation_Goal1Completions.json and
#Sessions_Page.json'))
with open('task/Goal1CompletionLocation_Goal1Completions.json') as data_file:
data_goal = json.load(data_file)
with open('skein_group_test/Sessions_Page.json') as data_file:
data_session = json.load(data_file)
pprint(data_session)
{u'reports': [{u'columnHeader': {u'dimensions': [u'ga:pagePath'],
u'metricHeader': {u'metricHeaderEntries': [{u'name': u'ga:sessions',
u'type': u'INTEGER'}]}},
u'data': {u'rows': [{u'dimensions': [u'/'],
u'metrics': [{u'values': [u'5925']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-_McBb_ag9eyaYD3mJjDG7G_uB3uhgGcsBxi15N7Dkx2CB6Q4EicvyM4qxz_QhoX49gt27HZU9461DeUyoMURHz597MYA&_hsmi=42202727'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-_ORc__Ui5VKSGvLO72LiNTFW98hg-mQlkIsltDFF4bZnKtFg4qvG21fhsL8zvY5rcQaRGke58VTsOsZTHhcAw7YWQMDvVnC8flaNVmXtZ3Pt9_sRI&_hsmi=45155107'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-_PMnaFvC1MTCPb_RghCCNcPyb6VHPUfs1AGacdBEc6k4WIkLbn_u24M5CG820anvWE4-jQa-2c2IEUNpl9Xqln0NruPA&_hsmi=42202727'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz--3SZ9_TxAeV_6dUflCeBmtEy9p5-kx7gJidd5Ibv-WOsLnzj_uCiZtdhRikZqTRif43CkxFcza6g657KSMlorVQiMBEg&_hsmi=45145424'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz--5qSlXatRwmauQbFfrahRUIfDCf0Sr03obAlY44VJRH5EId-A3BprJhTJXbEJ4KaVk0DPxxpp6r6sXB9RVDtgZeDJXeQ&_hsmi=45155107'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz--7jiNooHo7FlMd6hWQJ9lQ4YUTCEpBhXLKmoZXZwT8ceD_LFsoPxX3lxNtufY9-q6EQvsyGq0nstD-mq_LHIX3sUSUirBYOEP3ZIhOQ1zQkts8s2Y&_hsmi=45145424'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-8cXEz7dOC7R8Rn0Z1ursKJbEJXbIM9-J_DmmhjhMfNNtV-x7SIG0PTSiax05Wb5NQQs5lXV8J7VXQLkLFMROPILH01fw&_hsmi=45155107'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-8v6U0mczdHFIUhMyXIDy3wUfa-ZUxjJnQpdswsm8xGtqURMrq6SsUZDJ2Pd6JY9RHOrME0FHf2BZQX_GbQisqcaifxHg&_hsmi=45155107'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-9L_dXES2vJOjXvDV1KF1GweVEyHvneXaMmu_-a73CHsTcA8sBZ4nfsMSJZgE4BRwvWHdW3kCGFTordfdwC-_7cRgJbsQ&_hsmi=45145424'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/?_hsenc=p2ANqtz-9MN7HnoJ9kpuvsMGtK1ocWm95ICOHZYyOj8Y3vGUdV_ZqlQEgCQD9acCrN2_Ynz3dKDWr6kFJ0r9fYlxjyZCqr95UmLe0x5RBJKnbRobUgIsOz_4Y&_hsmi=42202727'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/apage'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/api/v1/page/html?roomHash=611fc30b6efe07edea8c301d0590d6eef9bd2c0f6feffa7f932bceb75c2d8e98&variation=A&responsiveMode=null'],
u'metrics': [{u'values': [u'3']}]},
{u'dimensions': [u'/BYTE'],
u'metrics': [{u'values': [u'5']}]},
{u'dimensions': [u'/coffeawrds'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/coffee'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/coffeeaward'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/CoffeeAwards'],
u'metrics': [{u'values': [u'13']}]},
{u'dimensions': [u'/coffeeawards.php'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/coffeeswards'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/cro'],
u'metrics': [{u'values': [u'4']}]},
{u'dimensions': [u'/en_us/article/google-wins-legal-battle-against-pro-trump-spammer-over-the-letter-g'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-_1UCYA4kBE9MhrKrjFFnG8wJi39lmmN--gjM_kcklm1Wehl4RGXV8ndu6_z3npbKyvClqbxJURi7vETChCZ8Z7SPrnvg&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-_7G__6Fu-hdAW_QDTCGsWHLFw-s0J0G0BaIsIu3wOkys-vre6bnNYsOAXda6F7PZke4Wne7ZB0cAJmMj7C1FeAIavg7-7ZnIMUPMoLgQLVyU45R8A&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-_KHuFCrySSUj6owG5YovjZ72Lm9Q3loiH70CIUdUaLNKavgM3dcBXLeom4Wn047Lck984ngSuBqsZTiT2M5xBoHNrWKBX9sO-8O9aztiWt-jYC_Cw&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-_kpjrIf3w91oSb6OZ1ffFcixc8rD38wVBF9OUaZO6xLw97wlKt-G0j14I3smruA5LgrCrSg1eJtUSoy3fjXZDzADkpTehFhS2kQ8v4-aOsq-88x5s&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz--EaTY5uMaCCENlHdZrqjWU87gsqIjB931oqY63YO16lzxdVOsHWdh9vwDC4oKLDbjgef1oqp7P7oiiU9mztVv9ZIQ06DeFoE83DnzAeng8ryrEvV0&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz--Fmzrt-is6YUkQDiyPm58tfNZI4Owhaobos17zqlKhCvvgmolMpZ4T9i8uN0diAotHwLyVzrLXpvdQNOyfZO7HalvQdA&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz--r-v1KNdidC5MRXxUbpNvKNUPZHrmc1BBM2L07bdEh9x0qmMaXCmyKhLW4JaqkJXug5MBWb-Vq4U4pRX6g8RAjEaJuIt-7fIihR3cytFlVY2N0acc&_hsmi=42202779'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz--yWxlJiqKw6qLxHn9HnAliavqXd5zkUGepQhAqizxaAzN34LOn8rEQ0Dm5RtSJzVZ30QYk4orMN3lyMgbM6JJfCewqvQYIiODjjfu47iKD67c2iic&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-86i5FI8qBJdD_t64nguhhjhOov-hf1IPpyV26yrEtWbMFya66lMUD4AWTt61AeXGWaM234bmzZanig122PbTm_mr_unQ&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-8n7M1UT4lKodEVHxtg_xPPAdosXCNKndXQRJ7vOEQbqhudVf-EMsbbw8tvEPHAwppxkH_nKKGtpVuyTgWln7tsfqyIcg&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-8r8DJGe2dOKVOvqLA0P41_zTaNrnqIAroB1PvSAZkNWC0JxdDd-hm02aOhBwaGjITXCtBfY_ni_t2Yei_hyrOBtAuLpg&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-9-ml0xW_uInx74E0sCS8Dr_QFbO2ioMoL-g5ysT4V5Q2V0iZdQvFCwNkGH9JdmAgVbABac-4RZMJdEwVWV9pZvxJjnWg&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-94T5q6sCVIPbizZXpy8nGenRB9lqiLfHeBfNuwGsVW6sUU9VPQWiV32BxAMODssrtK2xsx9R-nRuBXB9HlAolFUKMYFA&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-9NREL2hMY7qh4lQdA6lronzFISEUO9Q2OAvaL8P4wuFsYoTNpmRdyHVFysIJ2LQS5lr8EyFa9G3eL3zm9HtWZo27Vn-g&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/f?_hsenc=p2ANqtz-9op2ESwofMQMdqtCf_fL_UleMMStplEPtbDlamlXdXiIukiY7nYICCEo3xOW_ibAL1X76gYPFXAkNPBD-Z7FEQ6ULjW2PBHlXeoCLt10B2O5TnDK0&_hsmi=42202779'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/free-web-scan.php'],
u'metrics': [{u'values': [u'5']}]},
{u'dimensions': [u'/media/OurFabMembers/'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/media/rushes/'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/media/rushes/thumbnails/'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/ouramazingteam'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/ourfabmembers'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/portfolio-single-gallery.html'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/portfolio-single.html'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/printer'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/search?q=cache:9Se28Bsen5YJ:tmrw.co/+&cd=1&hl=en&ct=clnk&gl=uk'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/sharebutton.to'],
u'metrics': [{u'values': [u'10']}]},
{u'dimensions': [u'/staging/'],
u'metrics': [{u'values': [u'5']}]},
{u'dimensions': [u'/staging/2. live site/TMRW_Byte_Cafe.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/strike'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/subscribe.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/subscriube'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/tmrw_byte_cafe.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_Byte_Cafe.php'],
u'metrics': [{u'values': [u'270']}]},
{u'dimensions': [u'/TMRW_Byte_Cafe.php Croydon'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/tmrw_faqs.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_FAQs.php'],
u'metrics': [{u'values': [u'129']}]},
{u'dimensions': [u'/TMRW_Get_in_touch'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_Get_in_touch.php'],
u'metrics': [{u'values': [u'4']}]},
{u'dimensions': [u'/TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/private_office_in-coworking_space_TMRW_croydon_004.jpg'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/private-offices_in_coworking_space_tmrw_croydon_007.jpg'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_subscribe.php'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/tmrw_the_team.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_the_team.php'],
u'metrics': [{u'values': [u'140']}]},
{u'dimensions': [u'/TMRW_trainstrike_form.php'],
u'metrics': [{u'values': [u'3']}]},
{u'dimensions': [u'/TMRWpeople1.php'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/trainstrike'],
u'metrics': [{u'values': [u'2']}]},
{u'dimensions': [u'/trainstrike_form.php'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/trainstrikes.php'],
u'metrics': [{u'values': [u'231']}]},
{u'dimensions': [u'/trainstrikes.php?inf_contact_key=37d743fb0e8703a972f25e1bf88ba24e9c1670a04e6fad4bbf1dfc744b2373a7'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/trainstrikes.php?inf_contact_key=dd4202a772c196c27ca487b9d299fb4bc052e92cacf83cdfee0b5da024cd5b55'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/voteforbyte.php'],
u'metrics': [{u'values': [u'79']}]},
{u'dimensions': [u'/WeWork/Guardian1.jpg'],
u'metrics': [{u'values': [u'1']}]}],
u'totals': [{u'values': [u'6893']}]}}]}
In [107]:
pprint(data_goal)
{u'reports': [{u'columnHeader': {u'dimensions': [u'ga:goalCompletionLocation'],
u'metricHeader': {u'metricHeaderEntries': [{u'name': u'ga:goal1Completions',
u'type': u'INTEGER'}]}},
u'data': {u'isDataGolden': True,
u'maximums': [{u'values': [u'85']}],
u'minimums': [{u'values': [u'1']}],
u'rowCount': 7,
u'rows': [{u'dimensions': [u'(entrance)'],
u'metrics': [{u'values': [u'6']}]},
{u'dimensions': [u'/'],
u'metrics': [{u'values': [u'85']}]},
{u'dimensions': [u'/portfolio-single-gallery.html'],
u'metrics': [{u'values': [u'1']}]},
{u'dimensions': [u'/TMRW_Byte_Cafe.php'],
u'metrics': [{u'values': [u'22']}]},
{u'dimensions': [u'/TMRW_FAQs.php'],
u'metrics': [{u'values': [u'24']}]},
{u'dimensions': [u'/TMRW_Get_in_touch.php'],
u'metrics': [{u'values': [u'3']}]},
{u'dimensions': [u'/TMRW_the_team.php'],
u'metrics': [{u'values': [u'5']}]}],
u'totals': [{u'values': [u'146']}]}}]}
In [177]:
data_session_rows = data_session['reports'][0]['data']['rows']
data_goal_rows = data_goal['reports'][0]['data']['rows']
pd_session_rows = pd.io.json.json_normalize(data_session_rows)
pd_goal_rows = pd.io.json.json_normalize(data_goal_rows)[1:]
pd_goal_rows['dimensions'] = pd_goal_rows['dimensions'].astype(str)
pd_session_rows['dimensions'] = pd_session_rows['dimensions'].astype(str)
pd_session_rows.columns = ['dimensions','session_metrics']
pd_goal_rows.columns = ['dimensions','goal_metrics']
pd_conversion = pd_session_rows.set_index('dimensions').join(pd_goal_rows.set_index('dimensions'), how = 'inner')
def str_upd(test_val):
'''update page name'''
return test_val[2:-1]
def into_num(test_val):
'''convert string into int in json'''
return int(test_val[0]['values'][0])
pd_conversion.reset_index(inplace = True)
pd_conversion['dimensions'] = pd_conversion['dimensions'].apply(lambda x: str_upd(x))
pd_conversion.set_index('dimensions',inplace = True)
pd_conversion['session_metrics'] = pd_conversion['session_metrics'].apply(lambda x: into_num(x))
pd_conversion['goal_metrics'] = pd_conversion['goal_metrics'].apply(lambda x: into_num(x))
pd_conversion['conversion_rate'] = pd_conversion['goal_metrics']/pd_conversion['session_metrics']*100
In [179]:
pd_conversion['conversion_rate'].plot(kind = 'bar')
Out[179]:
<matplotlib.axes._subplots.AxesSubplot at 0xbb219e8>
In [180]:
print ("The best converting page on your site is "+
pd_conversion.sort_values(by = 'conversion_rate')[-1:]['conversion_rate'].index.values[0]
+ " with conversion rate "
+str(pd_conversion.sort_values(by = 'conversion_rate')[-1:]['conversion_rate'].values[0]) +
" %.")
The best converting page on your site is '/TMRW_Get_in_touch.php' with conversion rate 75.0 %.
In [ ]:
Content source: datahac/jup
Similar notebooks: