rgoogle-analyticscustom-dimensions

Can't access Custom Dimension in Google Analytics API via R


I'm attempting to create reports in R using the googleAnalyticsR package. We have 4 custom dimensions defined in Google Analytics, and when I try to access 2 of them, no data is retrieved.

In code below, When I use Dimension1 or Dimension2, everything runs smoothly. But if I try Dimension3 or Dimension4, no data is retrieved

library(googleAnalyticsR)
sampled_data_fetch2 <- google_analytics(ga_id, 
date_range = c("2018-01-01","2018-02-28"), 
metrics = c("sessions"), 
dimensions = c("date", "dimension2"))

2019-09-09 17:58:16> Downloaded [1000] rows from a total of [8079].

sampled_data_fetch2 <- google_analytics(ga_id, 
date_range = c("2018-01-01","2018-02-28"), 
metrics = c("sessions"), 
dimensions = c("date", "dimension4"))

2019-09-09 17:59:16> Downloaded [0] rows from a total of [].


Solution

  • I suspect that Dimension3 and Dimension4 are not session-scoped custom dimensions (hence the metrics sessions is not returning any results). The output of ga_custom_vars_list should confirm this (specifically the scope column):

    cds <- ga_custom_vars_list(
      accountId = account_details$accountId,
      webPropertyId = account_details$webPropertyId
    )
    
    head(cds)
    

    Output:

                 id accountId webPropertyId                          name index
    1 ga:dimension1  418XXXXX UA-XXXXX-5               CD1 NAME (1)     1
    2 ga:dimension2  418XXXXX UA-XXXXX-5               CD2 NAME (2)     2
    3 ga:dimension3  418XXXXX UA-XXXXX-5               CD3 NAME (3)     3
    4 ga:dimension4  418XXXXX UA-XXXXX-5               CD4 NAME (4)     4
    5 ga:dimension5  418XXXXX UA-XXXXX-5               CD5 NAME (5)     5
    6 ga:dimension6  418XXXXX UA-XXXXX-5               CD6 NAME (6)     6
        scope active             created             updated
    1 SESSION   TRUE 2014-02-18 18:42:23 2017-05-25 16:34:20
    2 SESSION   TRUE 2015-09-17 21:11:19 2017-05-25 16:34:29
    3     HIT   TRUE 2016-06-01 15:12:18 2016-06-01 15:12:18
    4     HIT   TRUE 2016-06-01 15:12:27 2017-05-25 16:36:24
    5     HIT   TRUE 2016-06-01 15:12:42 2017-05-25 16:36:29
    6     HIT   TRUE 2016-06-02 11:27:14 2016-06-02 11:27:14