代码之家  ›  专栏  ›  技术社区  ›  Randall Helms

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

  •  0
  • Randall Helms  · 技术社区  · 7 年前

    我在文档中使用了此页来帮助构建调用:

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

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

    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 回复  |  直到 7 年前
        1
  •  1
  •   XTOTHEL    7 年前