Chapter 9 Integrating Google Analytics
This chapter shows you how to integrate Google Analytics into your website to capture user data such as the number of users on your website, the pages they view, and their geographic locations. This chapter also covers the basics of the googleAnalyticsR R package for querying your Google Analytics data from R.
Note: This chapter builds upon the website generated in chapter 4 and edited in chapters 5, 6, 7, and 8. The final result can be viewed here - https://r4sites-anatole-custom.netlify.app/.
9.1 Prerequisites
Before you proceed, complete the following prerequisites:
- (optional) Discover your style
- Generate your website
- (optional) Fine tune the look and feel
- (optional) Configure pages and features
- Go live on the internet
- Sign up for Google Analytics
9.2 How it works
Once you sign up for Google Analytics, you will need to create an Account, Property, and Tracking ID for your website. Placing the Tracking ID into the config.yaml
file and triggering a fresh website deployment will complete the Google Analytics integration and enable you to being analyzing the data immediately.
9.3 Set up Google Analytics
Follow these steps to integrate Google Analytics into your website:
Sign in to Google Analytics and navigate to Admin > Create Account. Image
Under Account setup, provide an appropriate name (e.g. personal-website) and configure the Account Data Sharing Settings to your liking. The defaults should be fine. Click “Next.”
Under Property setup, provide an appropriate name (e.g. personal-website-property).
Under Property setup, click on “Show advanced options” to create a Universal Analytics property. For the Website URL, provide the URL to your website. Click “Next.” Image
Under About you business, configure the settings to your liking. Click “Next” and accept the terms of service.
Locate the Tracking ID by returning back to the Admin console and selecting the Account and Property that were created in the previous steps. Important: Make sure you locate the Universal Analytics property that reads
property name (UA-XXXXXXX-X)
and not the GA4 property. Under Property, click on Tracking Info > Tracking Code (Image). Copy the Tracking ID, which takes the form of UA-XXXXXXXXX-1. ImageReturn to your RStudio Project. Open
config/_default/params.toml
in the main directory and create agoogleAnalytics
parameter. Insert your tracking code as the parameter value. </> Code.
9.4 Push updates to GitHub
Any time you make changes to your website, such as integrating Google Analytics or authoring a new post, you will need to push these changes to your GitHub repository to force Netlify to rebuild and redeploy your website to the internet.
To complete the Google Analytics integration, open Git terminal (e.g. Git Bash on Windows, Terminal on Mac/Linux) and push your code to GitHub using the add > commit > push Git workflow.
# add all files to the local Git repository
git add --all
# commit files and include a brief message
git commit -m "brief message describing your changes"
# push files to GitHub
git push origin master
9.5 Confirm the integration
After completing the Google Analytics integration, run the following test to confirm the integration:
Open your website in your browser.
Sign in to Google Analytics.
Click on “All Web Site Data.” Image
In the dropdown menu, select the Account and Property you created during the initial setup, then select the “All Web Site Data” View. Image
Go to the Home page and locate the card titled “Active Users right now.” If the integration was successful, this value should be a minimum of 1 since you are currently on your website. Image
9.6 Browse the Google Analytics UI
You can analyze your Google Analytics data using a third party client, such as R, or by browsing the Google Analytics UI. The following steps will take you on a quick tour of the Google Analytics UI:
Sign in to Google Analytics.
Click on “All Web Site Data.” Image
In the dropdown menu, select the Account and Property you created during the initial setup, then select the “All Web Site Data” View. Image
Browse the Home page to view data such as active users, user acquisition sources, and sessions by country. Image
Browse the Reports (Image) to learn more about your users including how they arrive at your website and which pages they tend to view.
9.7 Use R for data analysis
googleAnalyticsR is an R package that makes it easy and fun to query your Google Analytics data. Before diving into the code, spend some time learning about Google Analytics Dimensions and Metrics.
9.7.1 Dimensions and metrics
In order to analyze your Google Analytics data, you need to gain a basic understand of Dimensions and Metrics.
Dimensions are attributes of your data and Metrics are quantitative measurements. For example, to analyze the count of users on your website in the last 60 days, the Dimension is date
and the Metric is users
.
Google Analytics supports hundreds of Dimension-Metric combinations. My recommendation is to browse the Ultimate Google Analytics Dimensions and Metrics List to learn what is available.
9.7.2 Install googleAnalyticsR
install.packages("googleAnalyticsR", dependencies = TRUE)
9.7.3 R packages for data analysis
library(googleAnalyticsR) ## querying your Google Analytics data
library(dplyr) ## data manipulation
library(plotly) ## interactive charts
library(DT) ## interactive tables
library(leaflet) ## interactive maps
9.7.4 Authenticate your Google Analytics account
Running ga_auth()
in the R console will take you to your web browser and prompt you to authenticate your Google account.
::ga_auth() googleAnalyticsR
9.7.5 List your Google Analytics Accounts and Properties
Running ga_account_list()
will list the Google Analytics account that was created in step 1 of Set up Google Analytics.
## get your accounts
<- ga_account_list()
ga_accounts
ga_accounts
## # A tibble: 2 x 10
## accountId accountName internalWebPrope… level websiteUrl type webPropertyId
## <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 143402170 abndistro 204792927 STAN… http://abnd… WEB UA-143402170…
## 2 143402170 abndistro 269232613 STAN… https://www… WEB UA-143402170…
## # … with 3 more variables: webPropertyName <chr>, viewId <chr>, viewName <chr>
9.7.6 Obtain the viewId associated with your website
To send API calls to Google Analytics, you need to obtain the viewId
associated with your website’s Account and Property name.
<- ga_accounts %>%
view_id filter(accountName == "abndistro", webPropertyName == "Abnormal Distributions") %>%
pull(viewId)
print(view_id)
## [1] "198103217"
9.7.7 Daily count of website users
To query the count of website users over a given date range, use the following Dimension and Metric:
- Dimension:
date
- Metric:
users
<- google_analytics(
daily_users viewId = view_id,
date_range = c("2019-07-01", "2019-12-31"),
metrics = "users",
dimensions = "date"
%>%
) as_tibble()
plot_ly(
data = daily_users,
x = ~date,
y = ~users,
mode = "lines"
%>%
) layout(title = "Daily count of website between June 2019 and December 2019")
9.7.8 Users by device type
To understand which device types your users are using to view your website, use the following Dimension and Metric:
- Dimension:
deviceCategory
- Metric:
users
<- google_analytics(
users_by_device_type viewId = view_id,
date_range = c("2019-01-01", "2019-12-31"),
metrics = "users",
dimensions = "deviceCategory"
%>%
) as_tibble()
plot_ly(
data = users_by_device_type,
x = ~deviceCategory,
y = ~users,
type = "bar"
%>%
) layout(title = "Most users are on desktop devices")
9.7.9 Page views by page
To measure the number of page views to each of your website pages, use the following Dimension and Metric:
- Dimension:
pageTitle
- Metric:
pageviews
<- google_analytics(
page_views viewId = view_id,
date_range = c("2019-01-01", "2020-12-31"),
metrics = "pageviews",
dimensions = "pageTitle"
%>%
) as_tibble()
%>%
page_views arrange(desc(pageviews)) %>%
::datatable() DT
9.7.10 Users by geographic location
To understand the geographical areas from which your website users view your website, use the following Dimension sand Metric:
- Dimensions:
latitude
,longitude
- Metric:
users
<- google_analytics(
users_location viewId = view_id,
date_range = c("2019-01-01", "2020-12-31"),
metrics = "users",
dimensions = c("latitude", "longitude"),
anti_sample = T
%>%
) as_tibble() %>%
mutate_at(vars(latitude, longitude), list(as.numeric))
%>%
users_location leaflet() %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = ~log(users),
stroke = FALSE,
fillOpacity = 0.5
)
9.7.11 Avoiding data sampling
The google_analytics()
function has an argument named anti_sampling
, which is set to TRUE
by default. This means that queries which return more than 1,000 rows of data will be sampled to avoid putting strain on the Google Analytics API. To avoid this default sampling strategy, set anti_sampling = FALSE
when making API calls using the google_analytics()
function.