The case: We are running a test on 5% of page views (NOT USERS) with a different ad layout. We are looking to tag each page view that has the different ad layout with a custom dimension.
The custom dimension scope is "Hit".
The problem we're running into: Google Analytics reporting is showing more than 5% of page views with the custom dimension. We know (via other reporting) that the different ad layout is only being shown to 5% of traffic (so Google Analytics is overreporting).
I'm assuming this is happening because the dimension we're setting is staying for subsequent page views (i.e. once a user has been tagged with that dimension, all page views after that are tagged). This is almost certainly due to a misunderstanding of how dimensions work and/or the code.
The code:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID', {
'custom_map': {'dimension1': 'adlayout'}
});
Later on in code:
gtag('event', 'adlayout_dimension', {'adlayout': 'true'});
Questions:
The event is firing after the pageview and the custom dimension is associated with the event.. not the pageview.
To fix it going forward, pass the custom dimension with the pageview itself rather than an event. To do so, edit the tracking code for the relevant pages similar to the following
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID',{
"dimension1": "true"
});
</script>
You can then create a custom report using a flat table, with Page and the Custom Dimension as dimensions together with the desired metrics.