Khashir
Khashir

Reputation: 351

How to knit dynamic reports with Google Analytics (rga)

I'm using rga to get some data from Google Analytics. From the repo:

The principle of this package is to create an instance of the API Authentication, which is a S4/5-class (utilizing the setRefClass). This instance then contains all the functions needed to extract data, and all the data needed for the authentication and reauthentication. The class is in essence self sustaining.

The package creates and saves a local instance using:

rga.open(instance="ga", where="~/ga.rga")

When I try to knit, however, I get an error that the ga object (what would be the instance) is not found. The code works when I run the chunks in RStudio, however—I believe the error is related to this aspect:

[The command above] will check if the instance is already created, and if it is, it'll prepare the token. If the instance is not created [...] it will redirect the client to a browser for authentication with Google.

My guess is that knitr can't perform that last step and so, the object is never created.

How can I make this work? I'm thinking that there might be a way to load the local ga.rga file to bypass browser authentication.

Upvotes: 0

Views: 63

Answers (1)

AnkDasCo
AnkDasCo

Reputation: 1599

You can bypass browser authentication by passing the client id and client secret key that you can get it from Google API console. Saving a local auth file in the dev env is always risky. You can try this code, this uses Google API and also saves the local instance -

rga.open(instance = "ga", 
     client.id = "<contains apps.googleusercontent.com>", 
     client.secret =<your secret key>, where ="~/ga.rga" )

Also ensure that desktop option setting is enabled in Google API console

Upvotes: 1

Related Questions