Julien Massardier
Julien Massardier

Reputation: 1466

Refresh Google Analytics token in R

I am using the following code to retrieve Google Analytics token in R. It works when I get the original token, but once it expires I am not able to refresh it. I get

Error: Refresh token not available

after ValidateToken(token). What am I doing wrong?

#get and store token 
require("RGoogleAnalytics")
token <- Auth(client.id,client.secret)
save(token,file="./token_file")

#Get refresh token
load("token_file")
ValidateToken(token)

Upvotes: 4

Views: 1823

Answers (1)

Omar Gonzales
Omar Gonzales

Reputation: 4008

I had to make a complete answer for this case.

RGA automatically refreshes the token, unless you specify it not to do so.

Run this lines, and use View(ga_profiles) to get the right ID you need. It is the ID for the View (Not the Account ID). In ga_profiles, is the first column.

library(RGA)

# get access token
authorize()

# get a GA profiles

ga_profiles <- list_profiles()

If you need (or want) that RGA ask for permissions every time you pull data use this code:

Notice the new.auth = TRUE argument.

library(RGA)

# get access token
authorize(new.auth = TRUE)

# get a GA profiles

ga_profiles <- list_profiles()

And to make a simple df with the data from Google Analytics:

id <- 88090889 #This ID is the first column from ga_profiles. Not the Account ID.

# get date when GA tracking began
first.date <- firstdate(id)


# get GA report data
ga_data <- get_ga(id, start.date = first.date, end.date = "today",
                  metrics = "ga:users,ga:sessions",
                  dimensions = "ga:userGender,ga:userAgeBracket")

If you need more help, post your code and we'll see.

Upvotes: 5

Related Questions