I found this question mentioned that the custom dimension value must set before ga('send', 'pageview');
But what if I use Google Tag Manager?
dataLayer.push({
'postId': '{{ $post->id }}',
'userId': '{{ auth()->user()->id }}',
});
I've set up the data layer variable to GTM to trigger GA. But when I look at the GA report, and filter by custom dimension, it's not working.
I checked with the debug, Tags Fired On This Event shows below
How do I set the custom dimension before it sent to GA pageview?
You would need to define the datalayer BEFORE loading of the GTM script.
Example to place at the top of the page before GTM is loaded:
<script>
dataLayer = [{
'postId': '{{ $post->id }}',
'userId': '{{ auth()->user()->id }}',
}];
</script>
When you do this, the data is available right away.
Another thing you can do is instead of using the datalayer, if the pages are rendered server-side, is to use tags with the postid and userid as values. This will ensure they're loaded before GTM.
https://developers.google.com/tag-manager/devguide#adding-data-layer-variables-to-a-page