Google Analytics is a powerful analytics solution that gives you really detailed insights into how your online content is performing. However, besides a tabular format and a data visualization tool, no other instruments are available to model your data and gain more powerful insights.
This is where R comes to help, and this is why the RGoogleAnalytics
package was developed: to provide a convenient way to extract data from Google Analytics into an R environment.
As an example, we will import data from Google Analytics into R regarding the daily bounce rate for a website in a given time range.
As a preliminary step, we are going to install and load the RGoogleAnalytics
package:
install.packages("RGoogeAnalytics") library(RGoogleAnalytics)
This can be easily obtained from (assuming that you are already logged in to Google Analytics) https://console.developers.google.com/apis.
After creating a new project, you will see a dashboard with a left menu containing among others the APIs & auth section, with the APIs subsection.
After selecting this section, you will see a list of available APIs, and among these, at the bottom-left corner of the page, there will be the Advertising APIs with the Analytics API within it:
After enabling the API, you will have to go back to the APIs & auth section and select the Credentials subsection.
In this section, you will have to add an OAuth client ID, select Other, and assign a name to your app:
After doing that and selecting the Create button, you will be prompted with a window showing your app ID and secret. Take note of them, as you will need them to access the analytics API from R.
Auth()
function, providing the annotated ID and secret:ga_token ← Auth(client.id = "the_ID", client.secret = "the_secret")
At this point, a browser window will open up and ask you to allow access permission from the app to your Google Analytics account.
After you allow access, the R console will print out the following:
Authentication complete
This web tool lets you experiment with query parameters and define your query before submitting the request from your code.
The basic fields that are mandatory in order to execute a query are as follows:
If you are going to further elaborate your data within your data project, you will probably find it useful to add a date dimension ("ga:date"
) in order to split your data by date.
Having defined your arguments, you will just have to pack them in a list using the init()
function, build a query using the QueryBuilder()
function, and submit it with the GetReportData()
function:
query_parameters <- Init(start.date = "2015-01-01", end.date = "2015-06-30", metrics = "ga:sessions, ga:bounceRate", dimensions = "ga:date", table.id = "ga:33093633") ga_query <- QueryBuilder(query_parameters) ga_df <- GetReportData(ga_query, ga_token)
The first representation of this data could be a simple plot of data that will result in a representation of the bounce rate for each day from the start date to the end date:
plot(ga_df)
Google Analytics is a complete and always-growing set of tools for performing web analytics tasks. If you are facing a project involving the use of this platform, I would definitely suggest that you take the time to go through the official tutorial from Google at https://analyticsacademy.withgoogle.com.
This complete set of tutorials will introduce you to the fundamental logic and assumptions of the platform, giving you a solid foundation for any of the following analysis.