I want to use the new googleAnalyticsR package to extract Google Analytics data using the v4 API.
The documentation (http://code.markedmondson.me/googleAnalyticsR/v4.html) demonstrates the execution of a query using one ga_id, but not using multiple view ids. There is another R package called GAR which permits the execution of multiple view id in a single Google Analytics query, but the googleAnalyticsR package includes v4 API features. I attempted to query multiple view ids using ga_id <- c('viewId','viewId'), but the query returns an error. Is there a way to query multiple view ids using googleAnalyticsR v4 API?
This probably isn't supported in API directly, but given you are using R, this could be very easily achieved using FOR loops. Below is an example where I am querying multiple GA views (1 view = 1 language version of the site):
viewId <- c(6006393, 79777098, 79781440, 79981805, 75315234, 78174757, 76630182, 79447058)
ga_data_final <- data.frame()
for (i in viewId) {
ga_data_temp <-
google_analytics_4(i, #=This is a (dynamic) ViewID parameter
date_range = c("2014-01-01",
"2016-08-15"),
metrics = c("sessions"),
dimensions = c("yearMonth",
"source",
"medium"),
max = -1)
ga_data_temp$viewId <- i
ga_data_final <- rbind(ga_data_final, ga_data_temp)
}
The code above retrieves:
1 metric: number of sessions
3 dimensions: yearMonth, Source, Medium
It's using 2 dataframes - the master one is created as empty before FOR loop starts. Every FOR cycle pulls rows for 1 view (temporarily stored in ga_data_temp) and once finished, appends them to the master dataframe (ga_data_final).
Hope this helps.
Related
Need to group some pages on my site together but apparently content grouping would only be forward facing. Is there a way to apply content grouping to prior data or create a calculated column as such which would allow GA to filter and group older data?
Is there a way to apply content grouping to prior data?
No, content grouping only applies going forward.
or create a calculated column as such which would allow GA to filter
and group older data?
No, any data you add to GA is only available as of the moment you create it.
What you can do:
GA:
Use Query Filters (not view filters) or segments: filters/segments don't create any data, they just filter it. So if you create 1 filter/segment per group and query the data, you will effectively have the same data as if you had done content grouping
Use Regex: GA supports regular expression which might help you create the filters you need
Use the API: since applying filters/segments might be tedious to do (and repeat) via the UI, you might want to use the API, for instance via the Google Sheets GA API add-on (see further details below)
Example of Google Sheets GA API add-on query to group some content that would match all pages which start with /foo or /bar (see list of API dimensions and metrics, see legacy filters syntax):
ga:pagePath=~^/(foo|bar)
Once you have figured out 1 query for 1 group you can clone them, query all your data, and then reaggregate it.
Google Data Studio:
GDS has a feature called Calculated fields which effectively allows you to create content groupings which apply to both historical and new data. Most likely you want to do this with a CASE statement:
CASE
WHEN REGEXP_MATCH(Page,'^/(foo|bar)') THEN "Group A"
WHEN REGEXP_MATCH...
ELSE "Other"
END
I'm trying to retrieve separately ga:userAgeBracket and ga:userGender using Google Analytics Report API v4 using a filter on eventCategory and eventAction.
From GA Dashboard, i'm able to retrieve the data even if there is only ~ 2.4k users and ~5.6k sessions. The repartition is 178 Males and 142 Females.
I'm trying to get the same result with the API but it's return nothing. I'm testing with https://ga-dev-tools.appspot.com/query-explorer with the same filters ect.
Is there any limit on the API ONLY when there is a small amount of data ? or another reason ?
EDIT: With another account with more data, and i still have the issue. Here are some screenshots
You created a segment in GA webUI and is comparing the results to "filters" in the explorer. You should be applying the same segment instead for consistency. Once you save the segment in webUI, it should be available in the "segment" field as a selection.
Your filtering also uses the "=~" regex match operator instead of the "=#" contains operator. Try this and you should have results.
I have multiple analytics accounts, I am facing problem with one of them. I am trying to fetch data through Analytics Core Reporting API v3. The metric combination selected are ga:avgPageLoadTime, ga:users and for other API call I removed the metric ga:users.
For some date ranges I can fetch data when ga:users is added to metric combination, but same API call without ga:users is not fetching any of the data.
metric combination #1:
ga:avgPageLoadTime, ga:users
metric combination #2:
ga:avgPageLoadTime
For some date ranges:
The data gets populated for metric combination #1, but gets no data for metric combination #2. Also with metric combination #2. I am also getting data when filtering is added.
Dimension selected is: ga:deviceCategory
I am trying to export Google Analytics data into R in order to build a report and do some other data mining related tasks with the data. I am using the RGoogleAnalytics package to do so. I have the connection working, but am having trouble specifying the correct query in order to obtain the right information.
I am trying to obtain information from a specific page that I would reach by going to the content drilldown section in google analytics, and searching for that specific page. I also would like to use a filtered view, to filter out ISP's that are from my work place. There are several websites under a particular view. To reach the specific page, I use the content drill down in Google Analytics. I am trying to build a query that pulls this information automatically. I have tried the following in regards to getting the correct query.
ValidateToken(token1)
query.list1 <- Init(start.date = "2016-10-28", end.date = "2016-12-05",
dimensions = "ga:date", metrics = "ga:uniquepageviews",
filters = "ga:pagePathLevel1
==/ed######.edu/;ga:pagePathLevel2==/content/",
table.id = "ga:##### ")
sort = "ga:date"
ga.query <- QueryBuilder(query.list1)
ga.data <- GetReportData(ga.query, token1)
This does not throw in error in R, but does not seem to be returning any metrics(it returns all zeros, for unique pageviews, when there are results) as shown below.
**date** **uniquepageviews**
1 20161028 0
2 20161029 0
3 20161030 0
4 20161031 0
In the above, I tried to use the filter to get the correct page. Is this correct? If so, what should I put into the filter so that it only returns metrics for a specific page in a given view? Also, is there a way to select for a given prebuilt view? Any help is appreciated, thanks.
Using R and the 'RGoogleAnalytics' package I have run into an issue of when running a query and the query in R not lining up with the table I get with the same segment in the Google Analytics report. I am using Google's Query Explorer to obtain the segment 'gaid', but I have also tried using the segment definition that the query explorer provides.
query.list <- Init(
start.date = "2015-5-1",
end.date = "2015-5-31",
dimensions = "ga:date",
metrics = "ga:users,ga:sessions",
segments = "gaid::...",
max.results = 10000,
sort = "ga:date",
table.id = "...")
ga.query<- QueryBuilder(query.list)
ga.data <- GetReportData(ga.query,oauth_token,
split_daywise = T)
The code runs, gives me a data frame, but the data frame does not match up with the tables I have with the same segment definitions in Google Analytics interface.
The segment of interest excludes specific users (defined by a custom user dimension), and has several session restrictions. The R code is is including extra users and sessions for some reason.
I've looked into the data trying to find out what users and sessions are getting through, but I am not finding any consistent trends between what is getting by the filter. Is anyone else running into this issue? Any suggestions?