【问题标题】:Using RGoogleAnalytics to retrieve data from google使用 RGoogleAnalytics 从谷歌检索数据
【发布时间】:2015-07-21 19:21:16
【问题描述】:

我正在使用 RGoogleAnalytics 检索多维数据,但每次我尝试运行 ga.data <- ga$GetReportData(query) 然后我收到一条错误消息: fromJSON(api.response.json, method = "C") 中的错误: 位置 53 的意外转义字符“\” 试试其他功能没问题 我该如何解决这个问题? 我使用以下代码:

require("RGoogleAnalytics")

query <- QueryBuilder()
access_token <- query$authorize()                                                

ga <- RGoogleAnalytics()

ga.profiles <- ga$GetProfileData(access_token)

profile <- ga.profiles$id[3] 
startdate <- "2013-10-01"
enddate <- "2013-12-31"
dimension <- "ga:date,ga:source,ga:medium,ga:keyword,ga:city,ga:operatingSystem,ga:landingPagePath"
metric <- "ga:visits,ga:goal1Completions,ga:goal3Completions"
sort <- "ga:visits"
maxresults <- 500000

query$Init(start.date = startdate,
           end.date = enddate,
           dimensions = dimension,
           metrics = metric,
           max.results = maxresults,
           table.id = paste("ga:",profile,sep="",collapse=","),
           access_token=access_token)

ga.data <- ga$GetReportData(query)

【问题讨论】:

  • 您应该发布更完整的代码,从所需的所有库调用开始,包括生成 query 的代码。

标签: r google-analytics-api


【解决方案1】:

我也遇到了一些麻烦,想出了一个办法。

第 1 步:安装软件包

# lubridate
       install.packages("lubridate")
# httr 
       install.packages("httr")
#RGoogleAnalytics

使用此链接下载此特定版本的 RGoogleAnalytics http://cran.r-project.org/web/packages/RGoogleAnalytics/index.html

第 2 步:创建客户端 ID 和机密 ID

  1. 导航到 Google Developers Console。 (https://console.developers.google.com/project)
  2. 创建一个新项目并打开它。
  3. 导航到 API 并确保已为您的项目启用 Analytics API。
  4. 导航到凭据并创建一个新的客户端 ID。
  5. 选择应用程序类型 - 已安装的应用程序。
  6. 创建您的客户端 ID 和客户端密码后,将它们复制到您的 R 脚本中。

    client.id <- "xxxxxxxxxxxxxxxxxxxxxxxxx"
    client.secret <- "xxxxxxxxxxxxxxx"
    token <- Auth(client.id,client.secret)
    

    为以后的会话保存令牌对象

    save(token,file="./token_file")
    

在以后的会话中,您不需要每次都生成访问令牌。假设您已将其保存到文件中, 可以通过下面的sn-p加载-

load("./token_file")

验证并刷新令牌

ValidateToken(token)

第 3 步:构建所需的查询

query.list <- Init( start.date = "2014-08-01",
                    end.date = "2014-09-01",
                    dimensions = "ga:sourceMedium",
                    metrics = "ga:sessions,ga:transactions",
                    max.results = 10000,
                    sort = "-ga:transactions",
                    table.id = "ga:0000000")

创建 Query Builder 对象以便验证查询参数

ga.query <- QueryBuilder(query.list)

提取数据并将其存储在数据框中

ga.data <- GetReportData(ga.query, token,paginate_query = FALSE)

方便的链接

常见错误:developers.google.com/analytics/devguides/reporting/core/v3/coreErrors#standard_errors

查询资源管理器: ga-dev-tools.appspot.com/query-explorer/?csw=1

维度和指标: developer.google.com/analytics/devguides/reporting/core/dimsmets

【讨论】:

    【解决方案2】:

    当 Rjson 库无法正确解析 Google Analytics JSON Feed 时,似乎会出现此错误。请试用 CRAN 最近发布和更新的 RGoogleAnalytics 库版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 2023-03-19
      • 2012-12-25
      • 2013-10-05
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多