Using a Google Analytics custom dimension for pageview statistics widget - google-analytics

I have a custom dimension that is being set with a gtag tracking code. I can actually see that the values are being send to GA and can, for example, make dashboard widgets with it.
The odd thing is that when I make a widget with sessions and the dimension it works well, but if I want to make a widget with pageviews and the dimension it returns no data at all.
This is my tracking code:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-0000000-0', {"anonymize_ip":true,"custom_map":{"dimension1":"premium"}});
gtag('set', 'premium', {'premium': 'premium post and not subscribed'});
I've also, instead of using 'set' tried using 'event' but this did not solve the issue. It looks like the custom dimension is not being collected with pageviews, but with sessions only.
Does anyone know if I have to configure it differently to also make it work for pageviews?

Since your custom dimension is configured at the hit level it needs to be associated with either an event or pageview hit. Your code looks like it simply sets the custom dimension value, but it's not followed by a pageview hit call. Try swapping the idea of your calls instead:
gtag('set', 'premium', {'premium': 'premium post and not subscribed'});
gtag('config', 'UA-0000000-0', {"anonymize_ip":true,"custom_map":{"dimension1":"premium"}});

Related

(not set) values from session_start events appearing for custom dimensions/parameters

We just added GA4 tracking to our website, along with custom parameters on events that were then configured as custom dimensions to be used in reports. After, we noticed "(not set)" values appearing when breaking out by a custom dimension. It appears as 0 Views, but roughly double the Total Sessions.
When filtered to "(not set)" for the custom dimension, and looking at the events, it seems these are mostly recorded as "session_start" and "first_visit" events.
Moreover, when filtering out "(not set)" for the custom dimension, the Sessions count for the page_view event does not add up to total, which leads me to think that there are Sessions with no page_view events - though I can't think of how this could happen on our site, and we don't have an app/any app tracking set up.
We compared and we get the same results from the API too.
Here is the code on our site (following the gtag guide here):
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXX"></script>
<script>
var ga4ParamsObject = JSON.parse(`{"classification":"Standard","example1":"value1","example2":"value2","example3":"value3"}`);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXX', ga4ParamsObject);
</script>
Questions:
Is something incorrect or missing from our implementation that's resulting in (not set) values appearing on custom dimensions?
Are we not "linking" session_start and first_visit events to page_view events within the session?
What can explain the Total Sessions being higher than the count of sessions with a page_view event?
It would appear that multiple parameters within the same session are counted twice when breaking out by these parameters (custom dimensions) - is that expected in GA4 or again something incorrect with our set up?
Additional notes:
We are running analytics.js in parallel
We've upgraded to 360
"Collect UA events in GA4" setting is turned off in Admin config
The GA4 property is connected to GAM, we serve ads on the page, and some advertisers do use gtag.js too, but I checked multiple times and our specific G- measurement ID only appears in the snippet I shared above
Here's an example page
Thanks to anyone reading this.
Custom parameters are currently not tracked with the session_start and first_visit automatically collected events which is why your custom parameters are (not set) with these two events.
Unfortunately this can impact your reporting depending on the metrics you use. E.g. if you use the views metric only and assuming your custom parameter is always set you will not see the (not set) value in your report. However if you use the users metric you will see the (not set) value as users will include events such as session_start where the custom parameter is not populated.

Cannot create reports for custom dimensions/metrics GA4

I have implemented a feedback star rating on my website at the bottom of each page. They can select one to five stars and optionally send free-form comments. I have implemented gtag.js to send the star rating integer and comments to my GA4 dashboard. Code snippet is below; I have omitted the JavaScript which allows the user to set the variables, but this is working.
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'MEASUREMENT_ID', {
'custom_map': {
'rating_dimension': 'rating_key',
'comments_dimension': 'comments_key'
},
'rating_key': 'this.value',
'comments_key': 'commentsValue',
});
</script>
gtag('event', 'rating_event', {'rating_key': this.value });
gtag('event', 'comments_event', {'comments_key': commentValue });
I went under Configure>Custom definitions and added the dimensions under the Event scope.
The data is being sent as an event, because this is something the user can optionally do on a page. I have verified that the data is sent to GA, I have the Google Chrome GA Debugger, and can see that it is arriving in the Realtime report under Events.
I would like to build a report which shows the page title/URL, the number of views, the length of time spent, along with its average star rating and any comments. I would like to have some adjustable controls so that I can configure this to be shown for a specified length of time. For example, if the page is modified, I can see the average rating before and after the change. Unfortunately, I am not able to build even the most rudimentary view of this data in the new GA web interface. I tried to modify various existing reports to include my custom dimensions, but they don't offer this option. I went under Explore and tried to add a Freeform report, but I can't get the data to show up there either, it only counts the number of times the event has occurred, which is not what I care about at all.
I have gone to a lot of effort to send the data to GA and had expected to be able to access the data and build a basic report.
Is it that you are unable to add custom dimensions and metrics to the free form exploration? Or when you want to add dimensions or metrics, they don't even show up in the custom section?
Normally you have to wait more or less (some say up to) 24 hours before being able to use custom definitions (metrics and dimensions) in any report.
this might not be your case, but I found your post looking for an answer to my problem, which is the custom metrics and dimensions are there, I am just unable to add them to the report, it looks like a bug, I click the + button, check the custom dimensions and nothing happens.

setting up user_id for Google Analytics

I followed the directions given when setting up the User-ID option.
I then created a view named 'user_id' for showing user_id info, but I am not seeing anything. When enabling User-ID, the code said to add the following to my tracking code.
gtag('set', {'user_id': '12345UserIdHere'});
However, when I go to the 'user_id' view, nothing is shown.
My current goal is to view which user ids were browsing the system during a given period of time.
Edit:
I am using this library:
https://www.googletagmanager.com/gtag/js?id=MY-ID-STUFF
I mostly used GTM, so I am not completely familiar with the gtag.js syntax, but I don't believe it has a "set" method. According to the documentation you'd have to set the user id in the "config" call instead:
gtag('config', 'GA_MEASUREMENT_ID', {
'user_id': 'USER_ID'
});
(Things in caps are placeholders).

Google Analytics is showing "Avg. Page Load Time" always as Zero

My google analytics script is like this
< !--Global site tag(gtag.js) - Google Analytics-- >
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></ script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxx-x', { 'page_path': curpath });
</script>
Thiw works fine except Avg. Page Load Time is not getting logged in analytics (always shows as zero). What am I missing here?
Note: My site works on ajax calls with single URL
You are missing the necessary information from the browser, since you use AJAX calls.
Google Analytics uses the Navigation Timing API, which is quite reliable, since the actual measurement is done by the browser and the values can be read as properties of the window.performance.timing object.
Example from here:
Calculate the total page load time
const perfData = window.performance.timing;
const pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;
Your Ajax call does not populate those values (which in a way makes sense, since the corresponding DOM events are not populated in an AJAX call), so GA cannot log a page load time.
You can do custom user timings in GA. They will be limited to a sample of 1% of your calls max, and your averages will be extrapolated from there. You would pass in the delta between the start of your Ajax call and the point at which the response is rendered.

Google Analytics Global Site Tag Custom Parameters

According to Google's gtag.js guide, it seems like we're able to define custom parameters. However, when using the code examples, only the Event Action gets populated. The Event Label is recorded in Google Analytics as "(not set)" and the Event Category as "general".
Code Example from developers.google.com:
gtag('event', 'video_play', {
'video_title': 'My promotional video',
'duration': '01:32'
});
It's also interesting to note that I cannot figure out how to show custom parameters as the columns in Google Analytics seem to be statically set to "Event Category", "Event Action", and "Event Label". These correspond to the default keys of "event_category", "event_action", and "event_label". Using these keys sends the values correctly. The following code works:
gtag('event', 'redirect', {
'event_category': 'Announcements',
'event_label': '/announcements/index.jsp',
Has anyone gotten custom parameters to work or is this a feature that hasn't been implemented yet in gtag.js? Is there additional configuration needed that I may have missed?
If you you were thinking of GA Custom Dimensions and Custom Metrics, yes it is available in the gtag.js / Global Site Tag syntax, see
https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets
in the form of a Map of CD indexes and attribute explicit names, followed by setting values to explicit attribute names.
for example
// Maps 'dimension2' to 'age'.
gtag('config', 'GA_MEASUREMENT_ID', {
'custom_map': {'dimension2': 'age'}
});
// Sends an event that passes 'age' as a parameter.
gtag('event', 'age_dimension', {'age': 55});
See also https://developers.google.com/analytics/devguides/collection/gtagjs/migration#custom_dimensions_and_metrics
However, gtag.js is a wrapper to make analytics.js easier to implement by hiding some its complexity.
If you are used to analytics.js, keep using it, you get more control on its behavior.
Or move to GTM, it's way more flexible.

Resources