通过GA Management API和R更新Google Analytics(分析)自定义维度

时间:2018-09-21 14:13:35

标签: r google-analytics google-analytics-api

我一直在尝试用R编写脚本以通过Google Analytics(分析)Management API以编程方式更新Google Analytics(分析)自定义维度(或指标)。

我已在文档中使用此页面来帮助建立呼叫:

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

主要问题是我可以发送呼叫,但响应是404状态,似乎什么也没回来。奇怪的是,如果我使用相同的参数尝试使用文档页面上的“尝试使用此API”部分,则可以更改维度名称。看到这里:

Successful Google Management API test

这是我一直在使用的R代码:

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'
                     )
         )

这就是我得到的结果:

call$status_code

#404

content <- content(call,'parsed')

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

1 个答案:

答案 0 :(得分:1)

请求方法应为PUT而不是POST。试试吧。

相关问题