Web analysis with R - part 2

Web analysis with R part 2

Connecting R with Google Analytics

R Logo verknüpfen mit Google Analytics

In our second blog post from the series Web Analytics with R we want to show how the programming language R can be linked with the tracking tool Google Analytics. Through this blog post you should be able to perform further web analytics with R.

In the last post we explained how packages can be installed and loaded within R.

So if the package “googleAnalyticsR” is not yet installed on your computer, you should install it now. If you have forgotten whether you have already successfully installed the Google Analytics package, then simply enter the following command in the R editor after the prompt “>”:


A screen appears showing you all packages that have been installed so far.

If the Google Analytics package appears in the list, you should check whether it is loaded or “activated”. This is done via the function “search()” in the R-Editor.


In this case, the package is not loaded yet, so we load it with the command “library(“googleAnalyticsR”)”

Repeatedly typing “search()” should now display the Google Analytics package.


Connecting Google Analytics with R

Now that all packages are installed and loaded, enter the following command in the R editor: “ga_auth()”.

ga auth

A browser should then open and you can select a Google Analytics account in the familiar Google login page.

Google Konto auswählen

After selecting an account and agreeing to the data transfer, a new window will appear with a login code. You must copy this code and enter it after the prompt in R.

Now you can create an object (“my_ga_accounts”) in which all your Google Analytics accounts will be tabulated. The command for this is as follows:

GA Konten

In the next step you can display the created table. This is done via the function “View(Object name)”.

GA Konten

This will display all accounts and additional information in tabular form.

Meine Account Daten

At this point, the name of the account “accountName” you want to select and the corresponding “ViewID” (last column) are of particular importance.

To do this, create a variable to which the selected “ViewID” is assigned.

Konto Name

You can then start an initial query from the selected data view of a Google Analytics account:

GA Data Frame

In the query created here, a data frame named “GA_DataFrame_1” stores the page views from January 20 to January 21, 2018. The dimension here is “days”.

The expression “google_analytics_4” means that the data is pulled through the Google Analytics Reporting API v4.

Optionally, you could have omitted the “combine” function i.e. c() at this point for “metrics” and also for “dimensions”.

In the next blog post – Web Analytics with R Part 3 – we will launch more queries in Google and look at further functionality.


Stay up to date!

More blog posts

Mann im Anzug zeigt mit Stift auf ein durchsichtiges Glas mit Statistiken

A/B testing in web analytics

A/B testing in web analytics Online marketers are constantly on the lookout for innovative techniques and concepts that will help them take their marketing campaigns to the next level. The

Read more »
Mensch hält Handy in der linken Hand und vor ihm steht ein Laptop auf einem Tisch


POSITION ZERO: Is #1 not enough anymore? The main SEO for most people still is to rank as high as possible. But the first place is recently more and more

Read more »