In [1]:
%matplotlib inline

In [2]:
import pandas as pd
import json

In [4]:
def read_data_from_json(file):
    with open(file) as json_data:
        data_dict = json.load(json_data)
    data_dict = data_dict['reports'][0]['data']['rows']
    data = pd.DataFrame.from_dict(data_dict)
    
    data['dimensions'] = data['dimensions'].apply(lambda x: x[0])
    data['metrics'] = data['metrics'].apply(lambda x: x[0]['values'][0])
    
    print(data)
    return data

read_data_from_json('task_data/Sessions_Page.json')


                                           dimensions metrics
0                                                   /    5925
1   /?_hsenc=p2ANqtz-_McBb_ag9eyaYD3mJjDG7G_uB3uhg...       1
2   /?_hsenc=p2ANqtz-_ORc__Ui5VKSGvLO72LiNTFW98hg-...       1
3   /?_hsenc=p2ANqtz-_PMnaFvC1MTCPb_RghCCNcPyb6VHP...       1
4   /?_hsenc=p2ANqtz--3SZ9_TxAeV_6dUflCeBmtEy9p5-k...       1
5   /?_hsenc=p2ANqtz--5qSlXatRwmauQbFfrahRUIfDCf0S...       1
6   /?_hsenc=p2ANqtz--7jiNooHo7FlMd6hWQJ9lQ4YUTCEp...       1
7   /?_hsenc=p2ANqtz-8cXEz7dOC7R8Rn0Z1ursKJbEJXbIM...       1
8   /?_hsenc=p2ANqtz-8v6U0mczdHFIUhMyXIDy3wUfa-ZUx...       1
9   /?_hsenc=p2ANqtz-9L_dXES2vJOjXvDV1KF1GweVEyHvn...       1
10  /?_hsenc=p2ANqtz-9MN7HnoJ9kpuvsMGtK1ocWm95ICOH...       1
11                                             /apage       1
12  /api/v1/page/html?roomHash=611fc30b6efe07edea8...       3
13                                              /BYTE       5
14                                        /coffeawrds       1
15                                            /coffee       1
16                                       /coffeeaward       1
17                                      /CoffeeAwards      13
18                                  /coffeeawards.php       2
19                                      /coffeeswards       1
20                                               /cro       4
21  /en_us/article/google-wins-legal-battle-agains...       1
22                                                 /f       1
23  /f?_hsenc=p2ANqtz-_1UCYA4kBE9MhrKrjFFnG8wJi39l...       1
24  /f?_hsenc=p2ANqtz-_7G__6Fu-hdAW_QDTCGsWHLFw-s0...       1
25  /f?_hsenc=p2ANqtz-_KHuFCrySSUj6owG5YovjZ72Lm9Q...       1
26  /f?_hsenc=p2ANqtz-_kpjrIf3w91oSb6OZ1ffFcixc8rD...       1
27  /f?_hsenc=p2ANqtz--EaTY5uMaCCENlHdZrqjWU87gsqI...       1
28  /f?_hsenc=p2ANqtz--Fmzrt-is6YUkQDiyPm58tfNZI4O...       1
29  /f?_hsenc=p2ANqtz--r-v1KNdidC5MRXxUbpNvKNUPZHr...       2
..                                                ...     ...
45                             /portfolio-single.html       1
46                                           /printer       1
47  /search?q=cache:9Se28Bsen5YJ:tmrw.co/+&cd=1&hl...       1
48                                    /sharebutton.to      10
49                                          /staging/       5
50           /staging/2. live site/TMRW_Byte_Cafe.php       1
51                                            /strike       1
52                                     /subscribe.php       1
53                                        /subscriube       1
54                                /tmrw_byte_cafe.php       1
55                                /TMRW_Byte_Cafe.php     270
56                        /TMRW_Byte_Cafe.php Croydon       1
57                                     /tmrw_faqs.php       1
58                                     /TMRW_FAQs.php     129
59                                 /TMRW_Get_in_touch       1
60                             /TMRW_Get_in_touch.php       4
61  /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/...       2
62  /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/...       1
63                                /TMRW_subscribe.php       2
64                                 /tmrw_the_team.php       1
65                                 /TMRW_the_team.php     140
66                         /TMRW_trainstrike_form.php       3
67                                   /TMRWpeople1.php       2
68                                       /trainstrike       2
69                              /trainstrike_form.php       1
70                                  /trainstrikes.php     231
71  /trainstrikes.php?inf_contact_key=37d743fb0e87...       1
72  /trainstrikes.php?inf_contact_key=dd4202a772c1...       1
73                                   /voteforbyte.php      79
74                              /WeWork/Guardian1.jpg       1

[75 rows x 2 columns]
Out[4]:
dimensions metrics
0 / 5925
1 /?_hsenc=p2ANqtz-_McBb_ag9eyaYD3mJjDG7G_uB3uhg... 1
2 /?_hsenc=p2ANqtz-_ORc__Ui5VKSGvLO72LiNTFW98hg-... 1
3 /?_hsenc=p2ANqtz-_PMnaFvC1MTCPb_RghCCNcPyb6VHP... 1
4 /?_hsenc=p2ANqtz--3SZ9_TxAeV_6dUflCeBmtEy9p5-k... 1
5 /?_hsenc=p2ANqtz--5qSlXatRwmauQbFfrahRUIfDCf0S... 1
6 /?_hsenc=p2ANqtz--7jiNooHo7FlMd6hWQJ9lQ4YUTCEp... 1
7 /?_hsenc=p2ANqtz-8cXEz7dOC7R8Rn0Z1ursKJbEJXbIM... 1
8 /?_hsenc=p2ANqtz-8v6U0mczdHFIUhMyXIDy3wUfa-ZUx... 1
9 /?_hsenc=p2ANqtz-9L_dXES2vJOjXvDV1KF1GweVEyHvn... 1
10 /?_hsenc=p2ANqtz-9MN7HnoJ9kpuvsMGtK1ocWm95ICOH... 1
11 /apage 1
12 /api/v1/page/html?roomHash=611fc30b6efe07edea8... 3
13 /BYTE 5
14 /coffeawrds 1
15 /coffee 1
16 /coffeeaward 1
17 /CoffeeAwards 13
18 /coffeeawards.php 2
19 /coffeeswards 1
20 /cro 4
21 /en_us/article/google-wins-legal-battle-agains... 1
22 /f 1
23 /f?_hsenc=p2ANqtz-_1UCYA4kBE9MhrKrjFFnG8wJi39l... 1
24 /f?_hsenc=p2ANqtz-_7G__6Fu-hdAW_QDTCGsWHLFw-s0... 1
25 /f?_hsenc=p2ANqtz-_KHuFCrySSUj6owG5YovjZ72Lm9Q... 1
26 /f?_hsenc=p2ANqtz-_kpjrIf3w91oSb6OZ1ffFcixc8rD... 1
27 /f?_hsenc=p2ANqtz--EaTY5uMaCCENlHdZrqjWU87gsqI... 1
28 /f?_hsenc=p2ANqtz--Fmzrt-is6YUkQDiyPm58tfNZI4O... 1
29 /f?_hsenc=p2ANqtz--r-v1KNdidC5MRXxUbpNvKNUPZHr... 2
... ... ...
45 /portfolio-single.html 1
46 /printer 1
47 /search?q=cache:9Se28Bsen5YJ:tmrw.co/+&cd=1&hl... 1
48 /sharebutton.to 10
49 /staging/ 5
50 /staging/2. live site/TMRW_Byte_Cafe.php 1
51 /strike 1
52 /subscribe.php 1
53 /subscriube 1
54 /tmrw_byte_cafe.php 1
55 /TMRW_Byte_Cafe.php 270
56 /TMRW_Byte_Cafe.php Croydon 1
57 /tmrw_faqs.php 1
58 /TMRW_FAQs.php 129
59 /TMRW_Get_in_touch 1
60 /TMRW_Get_in_touch.php 4
61 /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/... 2
62 /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/... 1
63 /TMRW_subscribe.php 2
64 /tmrw_the_team.php 1
65 /TMRW_the_team.php 140
66 /TMRW_trainstrike_form.php 3
67 /TMRWpeople1.php 2
68 /trainstrike 2
69 /trainstrike_form.php 1
70 /trainstrikes.php 231
71 /trainstrikes.php?inf_contact_key=37d743fb0e87... 1
72 /trainstrikes.php?inf_contact_key=dd4202a772c1... 1
73 /voteforbyte.php 79
74 /WeWork/Guardian1.jpg 1

75 rows × 2 columns

Read data from json


In [5]:
completion = read_data_from_json('task_data/Goal1CompletionLocation_Goal1Completions.json')
sessions = read_data_from_json('task_data/Sessions_Page.json')


                       dimensions metrics
0                      (entrance)       6
1                               /      85
2  /portfolio-single-gallery.html       1
3             /TMRW_Byte_Cafe.php      22
4                  /TMRW_FAQs.php      24
5          /TMRW_Get_in_touch.php       3
6              /TMRW_the_team.php       5
                                           dimensions metrics
0                                                   /    5925
1   /?_hsenc=p2ANqtz-_McBb_ag9eyaYD3mJjDG7G_uB3uhg...       1
2   /?_hsenc=p2ANqtz-_ORc__Ui5VKSGvLO72LiNTFW98hg-...       1
3   /?_hsenc=p2ANqtz-_PMnaFvC1MTCPb_RghCCNcPyb6VHP...       1
4   /?_hsenc=p2ANqtz--3SZ9_TxAeV_6dUflCeBmtEy9p5-k...       1
5   /?_hsenc=p2ANqtz--5qSlXatRwmauQbFfrahRUIfDCf0S...       1
6   /?_hsenc=p2ANqtz--7jiNooHo7FlMd6hWQJ9lQ4YUTCEp...       1
7   /?_hsenc=p2ANqtz-8cXEz7dOC7R8Rn0Z1ursKJbEJXbIM...       1
8   /?_hsenc=p2ANqtz-8v6U0mczdHFIUhMyXIDy3wUfa-ZUx...       1
9   /?_hsenc=p2ANqtz-9L_dXES2vJOjXvDV1KF1GweVEyHvn...       1
10  /?_hsenc=p2ANqtz-9MN7HnoJ9kpuvsMGtK1ocWm95ICOH...       1
11                                             /apage       1
12  /api/v1/page/html?roomHash=611fc30b6efe07edea8...       3
13                                              /BYTE       5
14                                        /coffeawrds       1
15                                            /coffee       1
16                                       /coffeeaward       1
17                                      /CoffeeAwards      13
18                                  /coffeeawards.php       2
19                                      /coffeeswards       1
20                                               /cro       4
21  /en_us/article/google-wins-legal-battle-agains...       1
22                                                 /f       1
23  /f?_hsenc=p2ANqtz-_1UCYA4kBE9MhrKrjFFnG8wJi39l...       1
24  /f?_hsenc=p2ANqtz-_7G__6Fu-hdAW_QDTCGsWHLFw-s0...       1
25  /f?_hsenc=p2ANqtz-_KHuFCrySSUj6owG5YovjZ72Lm9Q...       1
26  /f?_hsenc=p2ANqtz-_kpjrIf3w91oSb6OZ1ffFcixc8rD...       1
27  /f?_hsenc=p2ANqtz--EaTY5uMaCCENlHdZrqjWU87gsqI...       1
28  /f?_hsenc=p2ANqtz--Fmzrt-is6YUkQDiyPm58tfNZI4O...       1
29  /f?_hsenc=p2ANqtz--r-v1KNdidC5MRXxUbpNvKNUPZHr...       2
..                                                ...     ...
45                             /portfolio-single.html       1
46                                           /printer       1
47  /search?q=cache:9Se28Bsen5YJ:tmrw.co/+&cd=1&hl...       1
48                                    /sharebutton.to      10
49                                          /staging/       5
50           /staging/2. live site/TMRW_Byte_Cafe.php       1
51                                            /strike       1
52                                     /subscribe.php       1
53                                        /subscriube       1
54                                /tmrw_byte_cafe.php       1
55                                /TMRW_Byte_Cafe.php     270
56                        /TMRW_Byte_Cafe.php Croydon       1
57                                     /tmrw_faqs.php       1
58                                     /TMRW_FAQs.php     129
59                                 /TMRW_Get_in_touch       1
60                             /TMRW_Get_in_touch.php       4
61  /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/...       2
62  /TMRW_Images/TMRW_Gallery_Images/TMRW_Offices/...       1
63                                /TMRW_subscribe.php       2
64                                 /tmrw_the_team.php       1
65                                 /TMRW_the_team.php     140
66                         /TMRW_trainstrike_form.php       3
67                                   /TMRWpeople1.php       2
68                                       /trainstrike       2
69                              /trainstrike_form.php       1
70                                  /trainstrikes.php     231
71  /trainstrikes.php?inf_contact_key=37d743fb0e87...       1
72  /trainstrikes.php?inf_contact_key=dd4202a772c1...       1
73                                   /voteforbyte.php      79
74                              /WeWork/Guardian1.jpg       1

[75 rows x 2 columns]

Compute conversion rate


In [6]:
data = completion.merge(sessions, on='dimensions')
data.columns = ['Page','Goal1Completions','Sessions']
data[['Goal1Completions','Sessions']] = data[['Goal1Completions','Sessions']].apply(pd.to_numeric)
data['Conversion rate,%'] = data.apply(lambda row: 100*row['Goal1Completions']/float(row['Sessions']), axis=1)
data = data.sort_values(by='Conversion rate,%', ascending=False)
data


Out[6]:
Page Goal1Completions Sessions Conversion rate,%
4 /TMRW_Get_in_touch.php 3 4 75.000000
1 /portfolio-single-gallery.html 1 2 50.000000
3 /TMRW_FAQs.php 24 129 18.604651
2 /TMRW_Byte_Cafe.php 22 270 8.148148
5 /TMRW_the_team.php 5 140 3.571429
0 / 85 5925 1.434599

Get page with highest conversion rate


In [7]:
max_conv_rate = data.ix[data['Conversion rate,%'].idxmax()]
print('The best converting page on your site is \'%s\' with conversion rate %.2f%%.' %(max_conv_rate['Page'], max_conv_rate['Conversion rate,%']))


The best converting page on your site is '/TMRW_Get_in_touch.php' with conversion rate 75.00%.

Histogram


In [8]:
data.set_index('Page')['Conversion rate,%'].plot.bar(rot=40, legend=True)


Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x8d7fcd0>

In [ ]: