Friday, September 30, 2016

Google Analytics Package in R

Any analyst in the world knows what R and Python are. There is a new Package released in R.

Released by Mark Edmondson, It is called googleAnalyticsR. It uses the latest Version of the GA analytics API. Thus, any digital analyst, even those new to R can start using the power of R to get to the "what" of the business question. The code given below will pull Basic data for the past quarter and enable an analysts can the trend.
 # One time install of the packages required for this task  
 install.packages("ggplot2 ")  
 install.packages("plotly ")  
 install.packages("htmlwidgets ")  
 install.packages("googleAnalyticsR ")  
 #Add the basic Packages required for this task  
 library(ggplot2)  
 library(plotly)  
 library(htmlwidgets)  
 library(googleAnalyticsR)  
 # Set the view ID that we'll be using. You can get the view ID for a specific view from analytics edge or by logging into the Google Analytics Query Explorer at https://ga-dev-tools.appspot.com/query-explorer/. It's the "ids" value.  
 view_id <- XXXXXXXX  
 # Authorize Google Analytics. Use (new_user = TRUE) if you check multiple views  
 #ga_auth(new_user = TRUE)  
 ga_auth()  
 # Get the data from Google Analytics without Sampling  
 gadata <- google_analytics_4(view_id,   
                date_range = c("2016-08-01", "2017-03-31"),  
 metrics = "sessions", dimensions = c("date", "channelGrouping", "deviceCategory"),anti_sample = TRUE)  
 # Create your graphs  
 my_graph<- ggplot(gadata,aes(x=date,y=sessions))+stat_summary(fun.y=sum,geom = "bar")+facet_wrap(~channelGrouping + deviceCategory, scales = "free")  
 # Make Graphs interactive for exploration  
 ggplotly(my_graph)   
The output is a series of interactive graphs that looks like below. Like a dashboard , these will help an Analyst identify the most interesting stories to analyze.



Each line above has multiple functions so that any user can just insert the option they want. You can use the above to pull data across various accounts, dimension, metrics and customize the graph. Like I said earlier, R does not need introduction. Compared to excel, R does calculations much faster. Being open source, there are more open  source packages that we need. They enable powerful operations that save time on data arrangement and you do need a budget approval from managers to use it.

No comments: