Garftalk
Garftalk

Reputation: 31

Using RGoogleAnalytics to retrieve data from google

I'm using RGoogleAnalytics to retrieve mutiple dimensions data ,but every time I try to run ga.data <- ga$GetReportData(query) then I got an error message :Error in fromJSON(api.response.json, method = "C") : unexpected escaped character '\'' at pos 53 It's ok when I try other functions How could I fix this? I use the following code:

require("RGoogleAnalytics")

query <- QueryBuilder()
access_token <- query$authorize()                                                

ga <- RGoogleAnalytics()

ga.profiles <- ga$GetProfileData(access_token)

profile <- ga.profiles$id[3] 
startdate <- "2013-10-01"
enddate <- "2013-12-31"
dimension <- "ga:date,ga:source,ga:medium,ga:keyword,ga:city,ga:operatingSystem,ga:landingPagePath"
metric <- "ga:visits,ga:goal1Completions,ga:goal3Completions"
sort <- "ga:visits"
maxresults <- 500000

query$Init(start.date = startdate,
           end.date = enddate,
           dimensions = dimension,
           metrics = metric,
           max.results = maxresults,
           table.id = paste("ga:",profile,sep="",collapse=","),
           access_token=access_token)

ga.data <- ga$GetReportData(query)

Upvotes: 3

Views: 1121

Answers (2)

user4466316
user4466316

Reputation:

I had some trouble with this too,figured out a way.

Step 1: Install Packages

# lubridate
       install.packages("lubridate")
# httr 
       install.packages("httr")
#RGoogleAnalytics

Use this link to download this particular version of RGoogleAnalytics http://cran.r-project.org/web/packages/RGoogleAnalytics/index.html

Step 2: Create Client ID and Secret ID

  1. Navigate to Google Developers Console. (https://console.developers.google.com/project)
  2. Create a New Project and Open it.
  3. Navigate to APIs and ensure that the Analytics API is turned On for your project.
  4. Navigate to Credentials and create a New Client ID.
  5. Select Application Type – Installed Application.
  6. Once your Client ID and Client Secret are created, copy them to your R Script.

    client.id <- "xxxxxxxxxxxxxxxxxxxxxxxxx"
    client.secret <- "xxxxxxxxxxxxxxx"
    token <- Auth(client.id,client.secret)
    

    Save the token object for future sessions

    save(token,file="./token_file")
    

In future sessions, you need not generate the Access Token every time. Assumming that you have saved it to a file, it can be loaded via the following snippet -

load("./token_file")

Validate and refresh the token

ValidateToken(token)

Step 3: Build required query

query.list <- Init( start.date = "2014-08-01",
                    end.date = "2014-09-01",
                    dimensions = "ga:sourceMedium",
                    metrics = "ga:sessions,ga:transactions",
                    max.results = 10000,
                    sort = "-ga:transactions",
                    table.id = "ga:0000000")

Create the Query Builder object so that the query parameters are validated

ga.query <- QueryBuilder(query.list)

Extract the data and store it in a data-frame

ga.data <- GetReportData(ga.query, token,paginate_query = FALSE)

Handy Links

Common Errors: developers.google.com/analytics/devguides/reporting/core/v3/coreErrors#standard_errors

Query Explorer: ga-dev-tools.appspot.com/query-explorer/?csw=1

Dimensions and metrics: developers.google.com/analytics/devguides/reporting/core/dimsmets

Upvotes: 1

kushan_s
kushan_s

Reputation: 313

It seems that this error appears when the Rjson library isn't able to parse Google Analytics JSON Feed properly. Please try out the recently released and updated version of the RGoogleAnalytics library from CRAN.

Upvotes: 0

Related Questions