BICodeDatabaseGoogle AnalyticsGoogle BigQueryRTables

Push Google Analytics Data To Big Query

By December 19, 2018 No Comments

Get Google Analytics Data

Due to not so much documentation, I wanted to share my code for pushing unsampled Google Analytics Data directly to a Big Query Project only using R.

So here is the break down:

First we need to packages downloaded – for this use the install.packages command. Then we’ll load them into the project.

library(googleAnalyticsR)
library(bigQueryR)

We then use the Authentication function to get our Google Analytics and Google BigQuery data:

ga_auth()
bqr_auth()

Then, one can define a date_range_start and a date_range_end. By doing this we make a variable and make it easily changeable.

date_range_start <- "2018-12-01"
date_range_end <- "2018-12-31"

For the sake of the example, I have taken the sessions, page views and bouncerate. Change these to what you want to get from Google Analytics.

Overview <- google_analytics(12345678,
date_range = c(date_range_start,
date_range_end),
metrics = c("sessions",
"pageviews",
"bounceRate"),
dimensions = c("date"),
anti_sample = TRUE)

Now that we have our Google Analytics Data in a data frame, let us push it to Google BigQuery.

IMPORTANT!

For this to work, you need to create:

  • Create a Google Cloud Account
  • Create a new Big Query Project
  • Create a new Data Set
  • Create a new Table

Push Data To Google BigQuery

Now we’ll list our projects using, and after navigating to the project we want to push to:

projects <- bqr_list_projects()

my_project <- projects[1,1]

Then we do the same with our datasets:

datasets <- bqr_list_datasets(my_project)

my_dataset <- datasets[1,1]

And lastly, we will do the same with our newly created table:

tables <- bqr_list_tables(my_project, my_dataset)
my_table <- tables[1,1] Then all we need to do is using the following command to push it to BigQuery:
bqr_upload_data(my_project, my_dataset ,my_table, Overview)

Additional Information

Leave a Reply