The Management API v3 covers all API calls that are not data reporting related and are for getting meta information about your Google Analytics account or to change account settings.

Account structure

Shows the details for your account structure (Accounts > Web-properties > Views (profiles)) including creation time, user permissions etc.

The most day-to-day useful function is ga_account_list() which summarises all account web properties and views available to your user.

You can then also get the meta-data for that particular entity via ga_webproperty() or ga_view()

# ga_account_list is most commonly used
# (restricted to top 10 with the head() function)
head(ga_account_list(), n = 10)
##    accountId      accountName internalWebPropertyId    level
## 1   79301104          cloudyr             118427305 STANDARD
## 2   54516992     Demo Account              87479473 STANDARD
## 3   54516992     Demo Account              87479473 STANDARD
## 4   54516992     Demo Account              87479473 STANDARD
## 5   73050356     GDE Tracking             110873793 STANDARD
## 6   73050356     GDE Tracking             110873793 STANDARD
## 7   47480439    MarkEdmondson              78737968 STANDARD
## 8   47480439    MarkEdmondson              78742336 STANDARD
## 9   47584925   RaspberryPi UA              78875592 STANDARD
## 10  54019251 Sunholo Websites              86845257 STANDARD
##                                 websiteUrl webPropertyId
## 1                 http://cloudyr.github.io UA-79301104-1
## 2  https://shop.googlemerchandisestore.com UA-54516992-1
## 3  https://shop.googlemerchandisestore.com UA-54516992-1
## 4  https://shop.googlemerchandisestore.com UA-54516992-1
## 5                  http://gde-websites.com UA-73050356-1
## 6                  http://gde-websites.com UA-73050356-1
## 7                  http://markedmondson.me UA-47480439-2
## 8                  http://markedmondson.me UA-47480439-1
## 9                           http://erm.com UA-47584925-1
## 10        https://gwt-download.appspot.com UA-54019251-1
##             webPropertyName type starred    viewId          viewName
## 1       the cloudyr project  WEB      NA 123875646 All Web Site Data
## 2  Google Merchandise Store  WEB      NA  92320289     1 Master View
## 3  Google Merchandise Store  WEB      NA  92324711       2 Test View
## 4  Google Merchandise Store  WEB      NA  90822334   3 Raw Data View
## 5              GDE Tracking  WEB      NA 115760903      Bot excluded
## 6              GDE Tracking  WEB      NA 115751114          RAW DATA
## 7       MarkEdmondson oldGA  WEB    TRUE  81416156         Live Blog
## 8          markedmondson.me  WEB      NA  81416941 All Web Site Data
## 9               raspberryPi  WEB      NA  81569059 All Web Site Data
## 10             GWT Download  WEB      NA  90130533 All Web Site Data
# this only lists account meta-data
ga_accounts()
##         id             name             created             updated
## 1 47480439    MarkEdmondson 2014-01-25 12:17:05 2015-08-10 18:47:15
## 2 47584925   RaspberryPi UA 2014-01-29 17:01:47 2015-08-10 21:40:04
## 3 54019251 Sunholo Websites 2014-08-20 19:17:05 2015-08-10 19:50:44
## 4 54516992     Demo Account 2014-09-06 17:53:33 2018-02-11 23:40:54
## 5 73050356     GDE Tracking 2016-01-29 20:40:44 2016-01-29 20:40:44
## 6 79301104          cloudyr 2016-06-14 14:29:50 2016-06-14 14:29:50
##                            permissions.effective
## 1 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
## 2 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
## 3 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
## 4                               READ_AND_ANALYZE
## 5 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
## 6
# this gives meta-data for all web-properties for this accountId
ga_webproperty_list(47480439)
##              id accountId internalWebPropertyId                name
## 1 UA-47480439-1  47480439              78742336    markedmondson.me
## 2 UA-47480439-2  47480439              78737968 MarkEdmondson oldGA
##                websiteUrl    level profileCount    industryVertical
## 1 http://markedmondson.me STANDARD            1 HOBBIES_AND_LEISURE
## 2 http://markedmondson.me STANDARD            1 HOBBIES_AND_LEISURE
##   dataRetentionTtl dataRetentionResetOnNewActivity             created
## 1        MONTHS_26                            TRUE 2014-01-25 12:17:05
## 2       INDEFINITE                            TRUE 2014-01-25 12:19:20
##               updated defaultProfileId
## 1 2014-01-25 12:17:06             <NA>
## 2 2018-04-12 11:37:38         81416156
##                            permissions.effective
## 1 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
## 2 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
# this is meta-data for one particular web-property
ga_webproperty(accountId = 47480439, webPropertyId = "UA-47480439-1")
## $id
## [1] "UA-47480439-1"
## 
## $kind
## [1] "analytics#webproperty"
## 
## $selfLink
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439/webproperties/UA-47480439-1"
## 
## $accountId
## [1] "47480439"
## 
## $internalWebPropertyId
## [1] "78742336"
## 
## $name
## [1] "markedmondson.me"
## 
## $websiteUrl
## [1] "http://markedmondson.me"
## 
## $level
## [1] "STANDARD"
## 
## $profileCount
## [1] 1
## 
## $industryVertical
## [1] "HOBBIES_AND_LEISURE"
## 
## $dataRetentionTtl
## [1] "MONTHS_26"
## 
## $dataRetentionResetOnNewActivity
## [1] TRUE
## 
## $permissions
## $permissions$effective
## [1] "COLLABORATE"      "EDIT"             "MANAGE_USERS"    
## [4] "READ_AND_ANALYZE"
## 
## 
## $created
## [1] "2014-01-25T12:17:05.492Z"
## 
## $updated
## [1] "2014-01-25T12:17:06.625Z"
## 
## $parentLink
## $parentLink$type
## [1] "analytics#account"
## 
## $parentLink$href
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439"
## 
## 
## $childLink
## $childLink$type
## [1] "analytics#profiles"
## 
## $childLink$href
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439/webproperties/UA-47480439-1/profiles"
# this is meta-data for the views under this accountId/webPropertyId
ga_view_list(accountId = 47480439, webPropertyId = "UA-47480439-1")
##         id accountId webPropertyId internalWebPropertyId              name
## 1 81416941  47480439 UA-47480439-1              78742336 All Web Site Data
##   currency          timezone              websiteUrl type
## 1      USD Europe/Copenhagen http://markedmondson.me  WEB
##               created             updated eCommerceTracking
## 1 2014-01-25 12:17:05 2015-06-14 13:18:53             FALSE
##                            permissions.effective
## 1 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
# this is meta-data for this particular viewId (profileId)
ga_view(accountId = 47480439, webPropertyId = "UA-47480439-1", profileId = 81416941)
## $id
## [1] "81416941"
## 
## $kind
## [1] "analytics#profile"
## 
## $selfLink
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439/webproperties/UA-47480439-1/profiles/81416941"
## 
## $accountId
## [1] "47480439"
## 
## $webPropertyId
## [1] "UA-47480439-1"
## 
## $internalWebPropertyId
## [1] "78742336"
## 
## $name
## [1] "All Web Site Data"
## 
## $currency
## [1] "USD"
## 
## $timezone
## [1] "Europe/Copenhagen"
## 
## $websiteUrl
## [1] "http://markedmondson.me"
## 
## $type
## [1] "WEB"
## 
## $permissions
## $permissions$effective
## [1] "COLLABORATE"      "EDIT"             "MANAGE_USERS"    
## [4] "READ_AND_ANALYZE"
## 
## 
## $created
## [1] "2014-01-25T12:17:05.492Z"
## 
## $updated
## [1] "2015-06-14T13:18:53.638Z"
## 
## $eCommerceTracking
## [1] FALSE
## 
## $parentLink
## $parentLink$type
## [1] "analytics#webproperty"
## 
## $parentLink$href
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439/webproperties/UA-47480439-1"
## 
## 
## $childLink
## $childLink$type
## [1] "analytics#goals"
## 
## $childLink$href
## [1] "https://www.googleapis.com/analytics/v3/management/accounts/47480439/webproperties/UA-47480439-1/profiles/81416941/goals"

Helper functions

These are helper utility functions to work with Google Analytics data.

  • google_analytics_meta() - get a data.frame of available dimensions and metrics, or just use meta directly to get the version that comes with the library.
# you can just use `meta` as is to get the available metrics,
# here we just return the first 5 columns and rows for brevity
head(meta[,1:5])
##                      name      type dataType group     status
## 1             ga:userType DIMENSION   STRING  User     PUBLIC
## 2          ga:visitorType DIMENSION   STRING  User DEPRECATED
## 3         ga:sessionCount DIMENSION   STRING  User     PUBLIC
## 4           ga:visitCount DIMENSION   STRING  User DEPRECATED
## 5 ga:daysSinceLastSession DIMENSION   STRING  User     PUBLIC
## 6     ga:userDefinedValue DIMENSION   STRING  User     PUBLIC
# or ensure an up to date version by calling the metadata API.
head(google_analytics_meta())[,1:5]
## Warning: 'google_analytics_meta' is deprecated.
## Use 'ga_meta' instead.
## See help("Deprecated") and help("googleAnalyticsR-deprecated").
##                      name      type dataType group     status
## 1             ga:userType DIMENSION   STRING  User     PUBLIC
## 2          ga:visitorType DIMENSION   STRING  User DEPRECATED
## 3         ga:sessionCount DIMENSION   STRING  User     PUBLIC
## 4           ga:visitCount DIMENSION   STRING  User DEPRECATED
## 5 ga:daysSinceLastSession DIMENSION   STRING  User     PUBLIC
## 6     ga:userDefinedValue DIMENSION   STRING  User     PUBLIC
  • aggregateGAData() - aggregate data down to the dimensions you specify, treating metrics correctly regarding if they should be averaged or summed. This is used internally when using anti-sampling, and is also useful to respond to dimension selections in Shiny dashboards, where you don’t want to perform a new API call each time you need aggregated data. Download all data first in a more unaggregated form, then use this function.
# use `aggregateGAData` so you can on the fly create summary data
ga_data <- google_analytics(81416156, 
                            date_range = c("10daysAgo", "yesterday"),
                            metrics = c("sessions","bounceRate"), dimensions = c("hour","date"))
## 2019-03-26 10:30:38> Downloaded [240] rows from a total of [240].
head(ga_data)
##   hour       date sessions bounceRate
## 1   00 2019-03-16        8   75.00000
## 2   00 2019-03-17       21  100.00000
## 3   00 2019-03-18        9   77.77778
## 4   00 2019-03-19       14   78.57143
## 5   00 2019-03-20       27   74.07407
## 6   00 2019-03-21       11   81.81818
# if we want totals per hour over the dates:
aggregateGAData(ga_data[,c("hour","sessions")], agg_names = "hour")
## # A tibble: 24 x 2
##    hour  sessions
##    <chr>    <dbl>
##  1 00         125
##  2 01          85
##  3 02          87
##  4 03          83
##  5 04          77
##  6 05          77
##  7 06          83
##  8 07          91
##  9 08         106
## 10 09         158
## # … with 14 more rows
# it knows not to sum metrics that are rates:
aggregateGAData(ga_data[,c("hour","bounceRate")], agg_names = "hour")
## # A tibble: 24 x 2
##    hour  bounceRate
##    <chr>      <dbl>
##  1 00          79.9
##  2 01          67.3
##  3 02          81.1
##  4 03          76.0
##  5 04          76.4
##  6 05          73.0
##  7 06          82.2
##  8 07          83.7
##  9 08          88.2
## 10 09          77.0
## # … with 14 more rows
  • allowed_metric_dim() - Create named list of allowed GA metrics/dimensions. This is useful to have nice looking labels for dimension and metric selection pickers, or to create lookup tables.
#
amd <- allowed_metric_dim()
head(amd)
##                   Users               New Users          % New Sessions 
##              "ga:users"           "ga:newUsers" "ga:percentNewSessions" 
##      1 Day Active Users      7 Day Active Users     14 Day Active Users 
##          "ga:1dayUsers"          "ga:7dayUsers"         "ga:14dayUsers"

User management

  • ga_users_list() - list user access to your Google Analytics accounts, web properties or views
  • ga_users_delete() - delete user access via email
  • ga_users_delete_linkid() - delete user access via the linkId
  • ga_users_add() - add users to accounts
  • ga_users_update() - update a user

User management follows the recommended workflows outlined in this Google reference article.

The ga_users_* functions use the management APIs batching endpoint, to take advantage of the special increased quota limits when dealing with User management that API offers. For example, every 30 API calls that are batched that deal with user management only increments one API call against your management API quota. You can have 300 API calls in one batch (e.g. that costs 10 API calls against the quota), and googleAnalyticsR will split any list of emails into 300 emails per batch for you, so you can send in all emails you have.

By default, each Google Cloud Project gets 50 write requests per project per day, which if using batching means 1500 write/delete/update user operations per day.

You will need to use your own Google Cloud Platform project if using any write API operations seriously, otherwise you will be at the mercy of the shared googleAnalyticsR’s quota limits. See the section in the Setup page on how to setup using your own Google Project

To see which users are attached, use ga_users_list(). Each user email is referenced under the userRef.email column, whereas the unique IDs used to see which user is linked to where is the linkId which is unique to each user and link to your account, web property or view/profile.

Users can be attached at various levels in the hierarchy so you can call the function at various viewing angles.

# default will list all users that match the id you supply
ga_users_list(47480439)
ga_users_list(47480439, webPropertyId = "UA-47480439-2")
ga_users_list(47480439, webPropertyId = "UA-47480439-2", viewId = 81416156)

# only list users who have account level access
ga_users_list(47480439, webPropertyId = NULL, viewId = NULL)
# only list users who have webProperty and above access
ga_users_list(47480439, webPropertyId = "UA-47480439-2", viewId = NULL)

Users can be attached at account, web-property and view level.

Batching is incorporated for adding users, so you can pass in a vector of emails to add.

ga_users_add(c("the_email@company.com", "another_email@company.com"), 
             permissions = "EDIT", accountId = 47480439)

To delete a user from all accounts, web-properties and views takes a few API calls to ga_users_list() to find all the possible links, which is done behind the scenes of ga_users_delete(). You can pass the function multiple emails to delete many at once, which is done via batching:

ga_users_list(47480439)

ga_users_delete("the_email@company.com", 47480439)

# delete many emails at once
ga_users_delete(c("the_email@company.com", "another_email@company.com"), accountId = 47480439)

For more fine grained control, the below examples shows a workflow to list and delete user access for one user at View level using the linkId.

# get the linkId for the user you want to delete
ga_users_list(47480439, webPropertyId = "UA-47480439-2", viewId = 81416156)
ga_users_delete_linkid("81416156:114834495587136933146", 47480439, 
                webPropertyId = "UA-47480439-2", viewId = 81416156)
 
# check its gone
ga_users_list(47480439, webPropertyId = "UA-47480439-2", viewId = 81416156)

# can only delete at level user has access, the above deletion woud have failed if via:
ga_users_delete_linkid("47480439:114834495587136933146", 47480439)

You can also modify existing users via ga_users_update() - the below modifies on the web property level:

# the update to perform
o <- list(permissions = list(local = list("EDIT")))

ga_users_update("UA-123456-1:1111222233334444",
                update_object = o,
                accountId = 123456,
                webPropertyId = "UA-123456-1")

When using linkIds, make sure to use the correct linkId for the level of account, web-property or view you are operating on - the linkId starts with the accountId, UA code of web property, or ViewId number to distinguish between the various types.

Custom variables

Custom variable management for a Google Analytics property.

Using these you can scale setting up custom dimensions across your Google Analytics web properties. Custom dimension updates are governed by the same rules as when you create them in the web interface, as this Google article on custom dimensions and metrics explains.

You can’t delete custom dimensions, you can only de-activate them for reuse later on.

A workflow is shown below that creates, then updates a custom variable to a new name.

# create custom var
ga_custom_vars_create("my_custom_dim",
                      index = 15,
                      accountId = 54019251,
                      webPropertyId = "UA-54019251-4",
                      scope = "HIT",
                      active = FALSE)

# view custom dimension in list
ga_custom_vars_list(54019251, webPropertyId = "UA-54019251-4", type = "customDimensions")

# change a custom dimension
ga_custom_vars_patch("ga:dimension7",
                     accountId = 54019251,
                     webPropertyId = "UA-54019251-4",
                     name = "my_custom_dim2",
                     scope = "SESSION",
                     active = TRUE)

# view custom dimensions again to see change
ga_custom_vars_list(54019251, webPropertyId = "UA-54019251-4", type = "customDimensions")

AdWords

Custom Data Sources

See and upload custom data sources to Google Analytics

Experiments

View Filters

The filter edit functions are contributed by @zselinger which allow you to update filters for your Google Analytics views at scale.

Goals

Remarketing segments

Remarketing segments lets you target users in Google Ads from Google Analytics segments.

A demo of how you could use this based on your existing GA segments is:

adword_list <- ga_adwords_list(123456, "UA-123456-1")

adword_link <- ga_adword(adword_list$id[[1]])

segment_list <- ga_segment_list()$items$definition

my_remarketing1 <- ga_remarketing_build(segment_list[[1]], 
                      state_duration = "TEMPORARY",
                      membershipDurationDays = 90, 
                      daysToLookBack = 14)
my_remarketing2 <- ga_remarketing_build(segment_list[[2]], 
                      state_duration = "PERMANENT",
                      membershipDurationDays = 7, 
                      daysToLookBack = 31)

# state based only can include exclusions
ga_remarketing_create(adwords_link = adword_link,
                     include = my_remarketing1, exclude = my_remarketing2,
                     audienceType = "STATE_BASED", name = "my_remarketing_seg1")

Unsampled reports

Available only for GA360 accounts, you will need to authenticate with the Google drive scope to get download access. The download functions are contributed by @j450h1

Users