2

I want to use the new googleAnalyticsR package to extract Google Analytics data using the v4 API.

The documentation (http://code.markedmondson.me/googleAnalyticsR/v4.html) demonstrates the execution of a query using one ga_id, but not using multiple view ids. There is another R package called GAR which permits the execution of multiple view id in a single Google Analytics query, but the googleAnalyticsR package includes v4 API features. I attempted to query multiple view ids using ga_id <- c('viewId','viewId'), but the query returns an error. Is there a way to query multiple view ids using googleAnalyticsR v4 API?

4
  • You cannot do that in a single request because that is limitation of Analytics Reporting API V4 only. Commented Feb 11, 2017 at 4:59
  • Thanks dikesh. I was hoping maybe that maybe there was a way to query multiple ids as described with API v3 in (analyticsforfun.com/2015/05/…) Commented Feb 11, 2017 at 12:22
  • I understand but no API allows querying multiple view ids in single request. You have to run the loop to fire multiple queries and in each iteration pass the different view ids. Commented Feb 11, 2017 at 15:45
  • Its actually quicker in googleAnalyticsR to query multiple IDs via the v3 library as you can do as you demonstrate c(1234,2344,etc) but v4 doesn't allow batching outside one ID, so if you need v4 features you need to stick with a loop as the answer given below. Commented Feb 28, 2017 at 10:11

1 Answer 1

4

This probably isn't supported in API directly, but given you are using R, this could be very easily achieved using FOR loops. Below is an example where I am querying multiple GA views (1 view = 1 language version of the site):

viewId <- c(6006393, 79777098, 79781440, 79981805, 75315234, 78174757, 76630182, 79447058)    

ga_data_final <- data.frame()

for (i in viewId) {
  ga_data_temp <- 
    google_analytics_4(i, #=This is a (dynamic) ViewID parameter
                       date_range = c("2014-01-01",
                                      "2016-08-15"), 
                       metrics = c("sessions"), 
                       dimensions = c("yearMonth",
                                      "source",
                                      "medium"),
                       max = -1)
  ga_data_temp$viewId <- i
  ga_data_final <- rbind(ga_data_final, ga_data_temp)
}

The code above retrieves:

  • 1 metric: number of sessions
  • 3 dimensions: yearMonth, Source, Medium

It's using 2 dataframes - the master one is created as empty before FOR loop starts. Every FOR cycle pulls rows for 1 view (temporarily stored in ga_data_temp) and once finished, appends them to the master dataframe (ga_data_final).

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Peter Havlik. This approach worked very well.
Great to hear - would you mind accepting / upvoting the answer?
I tried to upvote, but my upvote does not register since my reputation is less than 15.
I think I've now accepted. Thanks again for the answer. It was very helpful.
Thanks, much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.