R script to import data from google analytics

前端 未结 1 1366
粉色の甜心
粉色の甜心 2021-01-05 20:20

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.

相关标签:
1条回答
  • 2021-01-05 20:36

    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")
    
    0 讨论(0)
提交回复
热议问题