Google Analytics with R - r

I am new to Google analytics API.....I authenticated my application in R using code:
library(RGoogleAnalytics)
client.id <- "**************.apps.googleusercontent.com"
client.secret <- "**********************"
token <- Auth(client.id, client.secret)
save(token,file="./token_file")
ValidateToken(token)
I am figuring out what we need to enter in the below credentials:
query.list <- Init(start.date = "2011-11-28",
end.date = "2014-12-04",
dimensions = "ga:date,ga:pagePath,ga:hour,ga:medium",
metrics = "ga:sessions,ga:pageviews",
max.results = 10000, sort = "-ga:date", table.id = "ga:33093633")
Where can I find dimensions, metrics, sort, table.id
My eventual goal is to pull the text from "https://plus.google.com/105253676673287651806/posts"
Please do assist me in this....

Using Google Analytics and R may not suit what you want to do here, as the Google+ website won't be included in the data you collect.
You may want to look at using RVest which is a URL scraper tool for R. You could then get the information you need from any public URL into an R dataframe for you to analyse later.

Query Explorer:
https://ga-dev-tools.appspot.com/query-explorer/?csw=1
Dimensions and metrics:
https://developers.google.com/analytics/devguides/reporting/core/dimsmets

Related

How to extract analytics from Adobe at URL filtered by channel using R

I'm trying to extract URL level data in a filtered channel (organic search) using the Adobe Analytics API and it's failing. How do I make it work?
I've tried using the RSiteCatalyst in R using the following code:
QueueDataWarehouse(my_rsid,
date.from = "2019-06-01",
date.to = "2019-06-30",
segment.id = "s300007365_5b366bb3f30aae698e8b0488",
elements = c("entrypage"),
metrics = c("uniquevisitors","averagetimespentonpage", "event123", "event38"),
enqueueOnly = F)
I expected a dataframe as defined in the code above and instead I get a dataframe that is NULL.
Please help.
PS While I'm here, is there another R package to work with the newer version of the API being 2.0?

Google Plus public search activities with keyword using R

I'm trying to fetch data from the Google Plus API but I only know how to search if I know the user_id.
Here's how I get the JSON using RCurl library:
data <- getURL(paste0("https://www.googleapis.com/plus/v1/people/",
user_id,"/activities/public?maxResults=100&key=", api_key),
ssl.verifypeer = FALSE)
I have tried formatting the URL like the documentation on google
like so:
data <- getURL(paste0("https://www.googleapis.com/plus/v1/activities/",
keyword,"?key=",api_key),ssl.verifypeer = FALSE)
but it doesn't work.
Is it even possible to search using a keyword from R or not? As R isn't in the supported programming languages for the API according to this link
I figured out how to make it work.
The GET request should be formatted as:
data <- getURL(paste0("https://www.googleapis.com/plus/v1/activities?key=",api_key,"&query=",search_string),ssl.verifypeer = FALSE)

Query multiple URLs from Google Analytics API using R

I got quite a big set of URLs (> 8.500) I want to query the Google Analytics API with using R. I'm working with the googleAnalyticsR package. The problem is, that I am indeed able to loop through my set of urls, but the dataframe created only returns the total values for the host-id for each row (e.g. same values for each row).
Here's how far I got to this point:
library(googleAnalyticsR)
library(lubridate)
#Authorize with google
ga_auth()
ga.acc.list = ga_account_list()
my.id = 123456
#set time range
soty = floor_date(Sys.Date(), "year")
yesterday = floor_date(Sys.Date(), "day") - days(1)
#get some - in this case - random URLs
urls = c("example.com/de/", "example.com/us/", "example.com/en/")
urls = gsub("^example.com/", "ga:pagePath=~", urls)
df = data.frame()
#get data
for(i in urls){
ga.data = google_analytics_4(my.id,
date_range = c(soty, yesterday),
metrics = c("pageviews","avgTimeOnPage","entrances","bounceRate","exitRate"),
filters = urls[i])
df = rbind(df, ga.data)}
With the result of always receiving the total statistics for the my.id-domain in each row in the dataframe created (own data):
Output result
Anyone knows of a better way on how to tackle this or does google analytics simply prevent us from querying it in such a way?
What you're getting is normal: you only queried for metrics (c("pageviews","avgTimeOnPage","entrances","bounceRate","exitRate")), so you only get your metrics.
If you want to break down those metrics, you need to use dimensions:
https://developers.google.com/analytics/devguides/reporting/core/dimsmets
In your case you're interested in the ga:pagePath dimension, so something like this (untested code):
ga.data = google_analytics_4(my.id,
date_range = c(soty, yesterday),
dimensions=c("pagePath"),
metrics = c("pageviews","avgTimeOnPage","entrances","bounceRate","exitRate"),
filters = urls[i])
I advise you to use the Google Analytics Query Explorer until you get the desired results, then port it to R.
As for the number of results, you might be limited to 1K by default until you increase max_rows. There is a hard limit on 10K from the API, which means you then have to use pagination to retrieve more results if needed. I see some examples in the R documentation with max=99999999, I don't know if the R library automatically handles pagination beyond the first 10K or if they are unaware of the hard limit:
batch_gadata <- google_analytics(id = ga_id,
start="2014-08-01", end="2015-08-02",
metrics = c("sessions", "bounceRate"),
dimensions = c("source", "medium",
"landingPagePath",
"hour","minute"),
max=99999999)

Extracting Google Analytics report for all pages

I'm trying to import data from Google analytic s to R by using rga library and following line.
myresults <- ga$getData(id, start.date="2015-04-28",
end.date="2015-05-28", metrics = "ga:exits",start = 1,max = 1000)
Above code works and does extract information specified by query "ga:exits", and I was wondering if there exits a query that would provide a report of page views for every pages.
P.S. I have tried google analytics query explorer.
Sincerely,
YJ
What about something like this:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxxxxxx&start-date=2015-05-01&end-date=2015-06-08&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews
Look for a dimensions: https://developers.google.com/analytics/devguides/reporting/core/dimsmets. For example, ga:pageTitle or ga:pagePath.
you should use ga:pagePath in the dimension and ga:pageviews in the metric.
rga.open(instance = "ga",
client.id = "xxxxxxxxxxxxxxgooleusercontent.com",
client.secret = "xxxxx-xxxxe46z_N")
ga$getData(ids,metrics="ga:pageviews", dimensions="ga:pagePath,
start.date="yyyy-mm-dd",
end.date="yyyy-mm-dd")
Hope this helps.

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)
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
Navigate to Google Developers Console.
(https://console.developers.google.com/project)
Create a New Project and Open it.
Navigate to APIs and ensure that the Analytics API is turned On for your project.
Navigate to Credentials and create a New Client ID.
Select Application Type – Installed Application.
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
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.

Resources