用于从谷歌分析导入数据的 R 脚本 [英] R script to import data from google analytics
问题描述
我正在尝试通过在 R studio 服务器上运行的脚本连接到 google analytic s API.
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/
如果我在本地主机上运行它,它工作正常,但是当我尝试通过 R studio 在远程服务器上运行脚本时,
If I run this on localhost, it works alright but when I try to run the script on a remote server through R studio,
授权步骤未完成,因为它尝试连接到本地主机上的 url,即
authroization step does not complete because it tries to connect to the url on localhost i.e
localhost:1410/
代替 REMOTESERVERHOSTNAME:1410
我发现这篇文章建议在通过 R studio 运行时进行端口转发:链接
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.
如何在不设置端口转发的情况下运行此脚本?还有哪些其他方法可以对我的 R 脚本进行 oauth 身份验证?
How to run this script without having to set port forwarding?What are other ways for oauth authentication for my R script?
推荐答案
一个建议是使用 Google 服务帐户.googleAuthR
包由 Mark Edmondson 提供,可通过 CRAN 获得,提供使用 Google 服务帐户在 R 中执行服务器端身份验证的功能.同一作者的另一个包 googleAnalyticsR
,也在 CRAN 上,与 googleAuthR
并使用生成的身份验证令牌执行针对 Google Analytics Reporting API 的查询,包括最新版本 4.0.
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.
要实现这一点:
- 为您的 Google API 项目创建一个服务帐户.
- 下载包含服务帐号私钥的 JSON 文件.
- 授予服务帐户访问 Google Analytics(分析)的权限,就像授予任何其他用户一样.
- 在使用
googleAuthR
进行身份验证时,将私钥 JSON 文件的位置作为参数提供(请参见下面的示例.):
- Create a service account for your Google API project.
- Download the JSON file containing the private key of the service account.
- Grant the service account access to Google Analytics, in the same way as you would for any other user.
- Supply the location of the private key JSON file as an argument when authenticating with
googleAuthR
(see the example below.):
以下示例 R 脚本引用包含私钥的 JSON 文件并执行基本的 Google Analytics 报告查询.请记住将 json_file
参数设置为适当的文件路径,并将 id
参数设置为适当的 Google Analytics 视图:
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屋!