0
votes

I have been I’m trying to write a script in R to programmatically update Google Analytics custom dimensions (or metrics) via the Google Analytics Management API.

I have used this page in the documentation to help build the call:

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/customDimensions/update

The main issue is that I can send the call but the response is a 404 status and nothing seems to come back. The strange thing is that if I try the ‘Try this API’ section on the documentation page with the same parameters I am able to alter the dimension name. See here:

Successful Google Management API test

Here is the R code that I have been using:

library(tidyverse)
library(httr)

ga_edit_auth <- function(client_id,client_secret) {

  myapp <- oauth_app("google", client_id,
                 client_secret)

  google_token <- oauth2.0_token(oauth_endpoints("google"), myapp,
                             scope =  "https://www.googleapis.com/auth/analytics.edit")

  google_token$init_credentials()

  return(google_token)
}

ga_token <- ga_edit_auth(id,secret)

#create the URL

api_url <- "https://www.googleapis.com/analytics/v3/management"
account_slug <- paste('/accounts/',account_id,sep='')
property_slug <- paste('/webproperties/',property_id,sep='')
dim_slug <- '/customDimensions/ga:dimension1'

post_url <- paste(api_url,account_slug,property_slug,dim_slug,sep = '')

#try to change the current dimension name value from 'old' to 'gold'

call <- POST(post_url,
         add_headers(Authorization = paste('Bearer', ga_token$credentials$access_token)),
         encode = 'json',
         body = list(kind = 'analytics#customDimension',
                     accountId = account_id,
                     webPropertyId = property_id,
                     name = 'gold',
                     index = 1,
                     scope = 'Hit',
                     active = TRUE,
                     id = 'ga:dimension1'
                     )
         )

Then these are the results I get back:

call$status_code

#404

content <- content(call,'parsed')

"
{xml_document}
<html>
[1] <body><p>Not Found</p></body>
"
1

1 Answers

1
votes

The request method should be PUT not POST. Try that.