Google Analytics Query for R, content drilldown - r

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.

Related

Google Analytics Data API (GA4) in R - Google Ads clicks

I use R Studio to export the necessary data from the GA4 property. I wanted to collect data on Google Ads campaign name, number of sessions and Google Ads clicks assigned to the corresponding campaigns. The number of sessions downloads to me, but it shows 0 Google Ads clicks, although I can see in the GA4 property that the clicks are there. Have you also had this problem? I am using the publisherAdclicks metric:
metric in R Studio API
I have not found any other metric through which I can retrieve this data, so do you have any idea what the problem is?
My code looks like this:
googleAdsSessionsClick <- google_analytics_set(c("sessions", "publisherAdClicks"),
c("sessionCampaignName", "sessionSourceMedium"))
I use my own function:
google_analytics_set <- function(choosenMetrics = NULL, choosenDimentions = NULL) {ga_data(webPropertyId,
date_range = date,
metrics = choosenMetrics,
dimensions = choosenDimentions)}

Difference between Google Analytics API v4 (no result) and dashboard

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.

Query multiple Google Analytics view ids using googleAnalyticsR v4 API package

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.

RGoogleAnalytics Segment not Agreeing with GA Web Output

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?

Google Analytics - How to filter by page, group by query string value?

I have a site with a store search that posts in the following format.
www.site.com/store-locator?city=&province=&zip[postal_code]=68123
I am trying to configure GA to give me feedback on people visiting this page and a count of specific zips searched.
example report data
/store-locator?city=&province=&zip[postal_code]=68123 1000 visits
/store-locator?city=&province=&zip[postal_code]=68456 768 visits
/store-locator?city=&province=&zip[postal_code]=68789 221 visits
note: the 'city' and 'province' values may also be populated (and I will want to mod GA to give similar data on these too).
Can anyone give feedback on how to configure GA to give me data similar to this?
Thanks!
As far as I know, the only way to look at this type of segment historically is using individual segments, which doesn't work well for an arbitrary number of zip codes. However, you can collect this data more effectively as described for new traffic. This comes up often with information like categories, tags, dates, query string variables, etc.
You can create Segments for each zip. This will work for historical analysis, but is impractical beyond a few. https://support.google.com/analytics/answer/3124493?hl=en&ref_topic=3123779
You can also use Content Grouping to create groups. This will not work historically. https://support.google.com/analytics/answer/2853423
The way I've handled this is using Custom Dimensions, which replaced Custom Variables when Universal came out. This also only works for future data.
To use Custom Dimensions, you would pass the zip code to google analytics explicitly when calling the analytics javascript code.
You can pull querystrings with javascript, or echo the parameter using something like PHP as follows:
<?php
if (array_key_exists("zip",$_GET)) { $theZip = $_GET["zip"]; }
else { $theZip = "nozip"; }
?>
And, sending the custom dimension --
ga('create', 'UA-XXXXX');
ga('set', {'dimension1': '<?php echo $theZip; ?>'})
ga('send', 'pageview');
You also need to setup the custom dimension in the Analytics Profile. Docs on custom dimensions https://developers.google.com/analytics/devguides/platform/customdimsmets
/store-locator?city=&province=&zip[postal_code]=68123 1000 visits
Step 1: In GTM, create a new macro. I called mine {{province}}
Macto Type = URL
Component Type = Query
Query Key = province
This will populate the Macro with the value of province from the query string.
Step 2: In your Google Analytics property, define a custom dimension called "province". This will assign an index key to the dimension.
Step 3: In your GTM tag for Google Analytics, you will find Custom Dimensions under more settings. Add a new dimension, apply the index number from #2 and for the dimension select the macro you created from #1
Publish and you are all set.
Now when you look in Google Analytics, you can add a secondary dimension and choose your newly created custom dimension.

Resources