Thursday, March 30, 2017

Extract Google Analytics Data using R

Google Analytics is arguably one of the most powerful web analytics service offered by Google tracks and reports website traffic. Google Analytics is a free Web analytics service that gives insights and fundamental analytical tools for website improvement (SEO) and promoting purposes. The administration is accessible to anybody with a Google account. Google purchased Urchin Software Corporation in April 2005 and utilized that organization's Urchin on Demand item as the reason for its present service.


Features of Google Analytics you should be know

1.      Custom Reports and Dashboards
2.      Share a new filter in multiple Views
3.      Set up your Goals at your view level
4.      Google Analytics Segments for analysis of subsets, such as conversions
5.       Emails based sharing and communication

6.      Provide facility to integrate with other google products, such as AdWords, Public Data Explorer etc.

      Through the Google Analytics Dashboard, clients can gather data on individual’s person whose sites connection to long range social networking websites, for example, Facebook and Twitter.
Is it accurate to say that you are sick of spending a large portion of your day replicating information out of the Google Analytics interface to refresh that same old report? Fortunately, there is a lot of tools out there to help with this: Google Spreadsheets, Qlikview, Shuffle point, and Tableau, just to give some examples. One of my most loved free apparatuses is R.
R is an effective program for statistical analysis, visualizing and reporting. Using R Language we can easily access the Google Analytics API with writing a few lines of code. 

Enable the Google Analytics API
In the first place, you have to ensure the Google Analytics API settings are arranged effectively.

1.      Navigate to Google Developer Console and Create a new project


1.      After creating a project enable the Google Analytics API.
2.      Go to Credentials tab, and create a new client Id and secret key(It will give you 4 option like API key, OAuth client ID etc. you need to select one options according to your need)
3.      Select Application Type(E.g. In this example I select Other Option) and give the name of the Application
4.      A pop box is generated which shows you client ID and Client Secret Key.
Once the project is configured and the accreditations set prepared, we have to verify your Google Analytics Account with your application. This guarantees your application (R Script) can get to your Google Analytics information.

 Using RStudio
 In R (I will be using RStudio), Load the necessary packages.

library(RGoogleAnalytics)
Client.id <-"xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
Client.secret <- "xxxxxxxxxxxx89A7Fv"
token<-Auth(client.id, client.secret)

Now you need to authenticate, which is basically telling Google Analytics that you have a right to access this data.

ValidateToken(token)

By running this command, a web browser should pop up, confirm your application to access your data, Click “Allow”


Once validated you get a couple of tokens (Access Token and Refresh Token). An Access Token is affixed with every API ask for so that Google's servers realize that the solicitations originated from your application and they are legitimate.
Now, very next step is to get the profile ID of the Google Analytics which the information extraction is to be done. It can be found in Admin Panel of the Google Analytics UI.

# using init function to initiate the fetching process of data
query_list <- Init(start.date = "2016-07-01",
                                  end.date = "2017-03-28",
                                  metrics = "ga:bounces",
                                  dimensions = "ga:month, ga:year",
                                 sort = "ga:year",
                                 max.results = 1000,
                                 table.id = "ga:xxxxxx")

Before questioning for a set of dimensions and metrics, you might need to check whether they are perfect. This should be possible utilizing the Dimensions and Metrics Explorer

#using Query Builder and get report data for mapping
ga.query <-QueryBuilder(query_list)

# Extract the data and store it in a data-frame
Bounces_report <- GetReportData(ga.query, token)
View(Bounces_report)


Let take another example, suppose we want to fetch the New user and Returning user counts

query_list1 <- Init(start.date = "2016-07-01",
                                  end.date = "2017-03-28",
                                  dimensions = "ga:userType",
                                  metrics = "ga:users, ga:newUsers",
                                 max.results = 1000,
                                 table.id = "ga:xxxxxx")

#using Query Builder and get report data for mapping
ga.query1 <-QueryBuilder(query_list1)

# Extract the data and store it in a data-frame
UserData_report <- GetReportData(ga.query1, token)
View(UserData_report)


In this way you can fetch data from Google Analytics using various dimension and metrics in R. In case if API returns an error, here’s a guide to understanding cryptic error responses.
















No comments:

Post a Comment

Creating Compelling Pie Charts in Looker: A Step-by-Step Guide with Examples

Creating Compelling Pie Charts in Looker: A Step-by-Step Guide with Examples   In the realm of data visualization, pie charts are a clas...