R脚本从谷歌分析导入数据 [英] R script to import data from google analytics

查看:200
本文介绍了R脚本从谷歌分析导入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过在R studio服务器上运行的脚本连接到google analytic s API。



我遵循本教程中的步骤:



http://www.r-bloggers.com/how-to-extract-google-analytics-data-in-r-using-rgoogleanalytics/



如果我在localhost上运行它,它工作正常,但是当我尝试通过R studio在远程服务器上运行脚本时,

authroization步骤会执行不完整,因为它试图连接到本地主机上的URL,即

  localhost:1410 / 

而不是 REMOTESERVERHOSTNAME:1410



我发现这个帖子提示如果通过R studio运行端口转发: link



但如果t明天,如果我想在另一台主机上访问它,我不想先设置端口转发。



如何在不设置端口转发的情况下运行此脚本?我的R脚本的oauth身份验证的其他方法是什么?

解决方案

一个建议是使用 Google服务帐户 googleAuthR Mark Edmondson 提供的c $ c> 软件包提供了在服务器端进行身份验证的功能R使用Google服务帐户。同一作者的另一个名为 googleAnalyticsR 的软件包,也在CRAN上,与 googleAuthR 集成,并使用生成的身份验证令牌对Google Analytics Reporting API执行查询,其中包括最新版本4.0。



实现此目的:


  1. 为您的Google API项目创建一个服务帐户。

  2. 下载包含服务帐户私钥的JSON文件。

  3. 授予服务帐户访问Google Analytics的权限,方式与您可以为任何其他用户。

  4. 使用 googleAuthR 进行身份验证时,将私钥JSON文件的位置作为参数提供(请参阅):

以下示例R脚本引用包含私钥的JSON文件并执行基本的Google Analytics报告查询。请记住将 json_file 参数设置为适当的文件路径,将 id 参数设置为适当的Google Analytics视图:











$ b $ j $ j
json_file = API Project-xxxxxxxxxxxx.json,
scope =https://www.googleapis.com/auth/analytics


google_analytics(id =123456789 ,start =2016-06-01,end =2016-06-28)


I am trying to connect to google analytic s API through a script running on R studio server.

I have followed steps from this tutorial:

http://www.r-bloggers.com/how-to-extract-google-analytics-data-in-r-using-rgoogleanalytics/

If I run this on localhost, it works alright but when I try to run the script on a remote server through R studio,

authroization step does not complete because it tries to connect to the url on localhost i.e

localhost:1410/

instead of REMOTESERVERHOSTNAME:1410

I found this post which suggests port forwarding if running through R studio : link

but if tomorrow, if I want to access it on the other host computer, I would not want to set port forwarding first.

How to run this script without having to set port forwarding?What are other ways for oauth authentication for my R script?

解决方案

One suggestion would be to use a Google Service Account. The googleAuthR package by Mark Edmondson, available through CRAN, provides functionality to perform server-side authentication in R using a Google Service Account. Another package by the same author called googleAnalyticsR, also on CRAN, integrates with googleAuthR and uses the resulting authentication token to execute queries against the Google Analytics Reporting APIs, including the latest version, 4.0.

To achieve this:

  1. Create a service account for your Google API project.
  2. Download the JSON file containing the private key of the service account.
  3. Grant the service account access to Google Analytics, in the same way as you would for any other user.
  4. Supply the location of the private key JSON file as an argument when authenticating with googleAuthR (see the example below.):

The following example R script references the JSON file containing the private key and performs a basic Google Analytics reporting query. Remember to set the json_file argument to the appropriate file path and the id argument to the appropriate Google Analytics view:

library(googleAuthR)
library(googleAnalyticsR)

gar_auth_service(
  json_file = "API Project-xxxxxxxxxxxx.json",
  scope = "https://www.googleapis.com/auth/analytics"
)

google_analytics(id = "123456789", start = "2016-06-01", end = "2016-06-28")

这篇关于R脚本从谷歌分析导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆