通过GA管理API更新Google Analytics自定义维度&;R

通过GA管理API更新Google Analytics自定义维度&;R,r,google-analytics,google-analytics-api,R,Google Analytics,Google Analytics Api,我一直在尝试用R编写一个脚本,通过Google Analytics管理API以编程方式更新Google Analytics自定义维度(或指标) 我在文档中使用了此页面来帮助建立呼叫: 主要的问题是,我可以发送电话,但响应是404状态,似乎什么也没有回来。奇怪的是,如果我使用相同的参数尝试文档页面上的“trythisapi”部分,我就能够更改维度名称。请看这里: 以下是我一直使用的R代码: library(tidyverse) library(httr) ga_edit_auth <-

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

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

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

以下是我一直使用的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'
                     )
         )
库(tidyverse)
图书馆(httr)

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

哈哈,老兄,这太明显了!它甚至在文档中说得很对——我先编写了一个“插入”函数的代码,该函数使用POST,然后复制并调整它以进行“更新”,但没有做到这一点。我很困惑为什么insert有效而update无效!哈哈哦,亲爱的。我会试试的,听起来应该有用。哈哈,没问题。有时只需要另一双眼睛。希望这就是问题所在!是的,刚刚用PUT测试了一下,效果很好。感谢您提供另一双眼睛!
call$status_code

#404

content <- content(call,'parsed')

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