Last active
March 18, 2021 06:54
-
-
Save ryanpraski/c9414e3d2be9b88a1bce83f2925065be to your computer and use it in GitHub Desktop.
Google Analytics scroll depth tracking report using the googleAnalyticsR & tidyr package. Check out this blog post tutorial on how to use this script: http://www.ryanpraski.com/scroll-depth-tracking-analysis-with-google-analytics-r/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googleAnalyticsR) | |
library(tidyr) | |
#Authorized Google Analytics R- this will open a webpage | |
#You must be logged into your Google Analytics account on your web browser | |
ga_auth() | |
#Use the Google Analytics Management API to see a list of Google Analytics accounts you have access to | |
my_accounts <- google_analytics_account_list() | |
View(my_accounts) | |
#Use my_accounts to find the viewId. Make sure to replace this with your viewId. | |
my_id <- 94579701 | |
#Page View Query | |
df1 <- google_analytics_4(my_id, | |
date_range = c("2016-10-01", "2016-12-07"), | |
metrics = c("pageviews"), | |
dimensions = c("pagePath"), | |
anti_sample = TRUE) | |
#Event Query | |
df <- google_analytics_4(my_id, | |
date_range = c("2016-10-01", "2016-12-07"), | |
metrics = c("totalEvents"), | |
dimensions = c("pagePath","eventLabel"), | |
filtersExpression = c("ga:eventLabel=~%|#disqus"), | |
anti_sample = TRUE) | |
#Use tidyr to make long data wide- move row data to columns | |
df2 <-spread(df,eventLabel,totalEvents,fill = 0) | |
#merge dataframees with pageview metric with scroll depth metrics | |
df3 <-merge(df1,df2) | |
#calculate percentage of people reaching each 25, 50, 75, 100, disqus comment on the page scroll depth | |
df3$percent25 <-round(df3$`25%`/df3$pageviews,digits = 2) | |
df3$percent50 <-round(df3$`50%`/df3$pageviews,digits = 2) | |
df3$percent75 <-round(df3$`75%`/df3$pageviews,digits = 2) | |
df3$percent_disqus <-round(df3$`#disqus_thread`/df3$pageviews,digits = 2) | |
df3$percent100 <-round(df3$`100%`/df3$pageviews,digits = 2) | |
#remove the raw counts from dateframe for page scroll depth and only show percent reaching each page depth | |
df4<-df3[,c("pagePath","pageviews","percent25","percent50","percent75","percent_disqus","percent100")] | |
#sort dataframe by pageviews | |
df5<-df4[order(-df4$pageviews),] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment