I'm trying to set up a custom dimension for GA4 called chain_id
using the gtag set
method. I've also added a custom_map
property in the config but I'm not even sure if that is necessary. The issue is that the chain_id
is never being sent with the event as you can see in the console output from the GA debugger.
Not really sure what I'm missing here but I think that chain_id
with the value of Test chain id
should be present in event parameters.
document.getElementById("another").addEventListener("click", function(event) {
gtag("event", "Button click");
});
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<my-ga-id>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("set", {
chain_id: "Test chain id"
});
gtag("config", "<my-ga-id>", {
debug_mode: true,
custom_map: {
dimension1: 'chain_id'
}
});
</script>
<button id="another">Another click</button>
gtag set doesn't work. Well or at least it doesn't work as one would expect it to work. Google's documentation is off. Set changes the DL for GTM's benefit. It's not very useful for setting properties. But you have similar options.
You can just set the params within the event call itself, like so:
gtag("event", "Button click", {chain_id: "test id"});
The picture is from this page's console.
Note that Google made another sneak update for ignoring configs that are issued after the initial config has been used. So if you're trying to change persistent event parameters for all subsequent events by issuing a config and gtag.js just ignores it, then give this answer a read: can GA4 custom dimensions be updated after the initial 'config' call?
Overall, please consider using GTM. Or any other TMS. It makes tracking a lot easier to implement, manage, scale and support. Tracking directly with gtag.js only really makes sense when you have to do just a little bit of tracking and you don't really care much about what real business results this tracking achieves in the longer term.