Connect R with Google Analytics

Google Analytics is being used by analyst for various purposes, like who all are accessing their websites and at what time of day. What are the prominent keywords being entered in search criteria of webpage. It will be very helpful for analysts/professionals if they can directly import data from GA into for further analysis.
Method 1

install.package("RGoogleAnalytics")
require(RGoogleAnalytics)

## It need not be executed in each session as the token is saved in the working directory of R on your computer

token <- Auth(client.id="client Id",client.secret="Client Secret")
save(token,file="token_file")
## In future sessions it can be loaded as follows
 load("./token_file") ,
ValidateToken(token)
query.list<-Init(start.date="2017-5-30",
                         end.date ="2017-5-31",
                         dimensions = "ga:date,ga:hour",
                         metrics = "ga:sessions,ga:pageviews",
                         max.results=100000,
                         sort = "-ga:date",
                         table.id = "ga:table.id")
## Table ID is in the URL of your Google Analyics page. It is everything past the “p” in the URL. Example,  
 https://www.google.com/analytics/web/?hl=en#management/Setting/a48963421w80588688pTABLE_ID_NUMBER

ga.query <- QueryBuilder(query.list)
ga.data <- GetReportData(ga.query, token, split_daywise = T, delay = 5)

The data get saved in data fram ga.data.

 
 

1 comment:

  1. Thanks for sharing this blog Rakhi. This was very important list for students and professional as well. For further details about R Language

    ReplyDelete

Translate

Monte Carlo Simulation with R

Stochastic Modeling A stochastic model is a tool for modeling data where uncertainty is present with the input. When input has cert...