User flow - Conversions by pages

0. Import libraries


In [13]:
%matplotlib inline 

import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import pandas as pd

pd.set_option('display.width', 500)
pd.set_option('display.max_columns', 100)
pd.set_option('display.notebook_repr_html', True)
import seaborn as sns #sets up styles and gives us more plotting options

1. Settings


In [14]:
# Time period 1st Jan - 30th April (arbitrary )

# API credentials
# Email address 705762800217-compute@developer.gserviceaccount.com
# Key IDs 948ee8e2a420ef14a5d5a29bd35104fe2f1e6ed4

In [15]:
# open file. It is requested via API explorer using request parameters:

#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
#ga:sessionsWithEvent

#dimensions
#ga:pagePath

#sort
#-ga:sessionsWithEvent

#filter
#ga:sessions>10

2. Data import & transformation


In [16]:
# Open file
# original file exported from GA includes ga:pagePath,ga:sessions,ga:sessionsWithEvent
# Calculate "cr" as "Sessions with event"/"Sessions" for each page.

input_flow= pd.read_csv('files/TMRW_flow.csv')

# rename columns
input_flow.columns=['Page','Sessions','Conversions']

# filter page with CR > 0
input_flow = input_flow[input_flow.Conversions > 0]

# group by page
input_flow_index = input_flow.set_index('Page')
input_flow


Out[16]:
Page Sessions Conversions
0 '/ 4436 82
1 '/TMRW_FAQs.php 100 26
2 '/TMRW_Byte_Cafe.php 218 23
3 '/TMRW_the_team.php 99 10
  1. Calculation

In [17]:
#calculatingCR
input_flow['CR'] = input_flow.Conversions / input_flow.Sessions * 100
input_flow


Out[17]:
Page Sessions Conversions CR
0 '/ 4436 82 1.848512
1 '/TMRW_FAQs.php 100 26 26.000000
2 '/TMRW_Byte_Cafe.php 218 23 10.550459
3 '/TMRW_the_team.php 99 10 10.101010

In [18]:
input_flow_cr = input_flow.set_index('CR')

max_CR = max(input_flow.CR)
best_page = input_flow_cr.loc[max_CR,'Page']
best_page


Out[18]:
"'/TMRW_FAQs.php"

4. Validate

5. Visualise


In [20]:
from bokeh.io import output_notebook
from bokeh.charts import Bar, Line, show
from bokeh.plotting import figure, output_file, show

output_notebook()
p = Bar(input_flow, 'Page', values='CR', title="The best converting page on your site is %s" % best_page)
p.legend.location = "top_right"
show(p)
print("Put an additional button \"Contact Us\" on the main page. (or adding the contact form link to the menu bar at the top of the homepage")


Loading BokehJS ...
Put an additional button "Contact Us" on the main page. (or adding the contact form link to the menu bar at the top of the homepage

In [ ]:


In [ ]: