Missing User ID when using Tag Manager
-
Hi,
I’m trying to use a Tag Manager tracking code (“Tag Manager” under “Matomo Analytics” > “Settings” > “Add Tracking Code”) and track the User ID at the same time (“WP User ID” under “User ID Tracking”).I have my container set up with a Tag, a DOM Ready Trigger & a Matomo Configuration Variable and working, but user IDs are not tracked. The User ID is empty under the Variable, so I looked into filling it as advised in https://matomo.org/faq/tag-manager/how-do-i-set-the-user-id-using-matomo-tag-manager/.
However, I noticed the user ID is not pushed to the MTM data layer in the generated code:
<script> window._paq = window._paq || []; window._paq.push(['setUserId', '1']); var _mtm = _mtm || []; _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.src="https://domain.tld/wp-content/uploads/matomo/container_ID.js"; s.parentNode.insertBefore(g,s); </script>
It seemed something like below would be more appropriate, so that it’s possible use the a
{{uid}}
variable in the tag manager:<script> window._paq = window._paq || []; window._paq.push(['setUserId', '1']); var _mtm = _mtm || []; _mtm.push({'uid':'1' }); _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.src="https://domain.tld/wp-content/uploads/matomo/container_ID.js"; s.parentNode.insertBefore(g,s); </script>
So I tried to check the available filters – I found
matomo_tracking_code_script
that looked promising, and I thought about using it to replicate what’s done withTrackingCodeGenerator::apply_user_tracking
, but unfortunately it’s not called byTrackingCodeGenerator::prepare_tagmanger_code
, it’s only called inTrackingCodeGenerator::prepare_tracking_code
.I think pushing this the User ID to the MTM layer and, at the very least, adding a filter similar to
matomo_tracking_code_script
inTrackingCodeGenerator::prepare_tagmanger_code
would be a huge improvement, and increase code architecture consistency.Please let me know if I’m wrong, but I feel that right now the only alternatives are either to manually change the tracking code to include both “Default Tracking” and “Tag Manager”, or to somehow output the user ID somewhere in the DOM and get it in Matomo Tag Manager, which to me is a solution that adds unnecessary coupling for the latter, and is far from straightforward in either case.
- The topic ‘Missing User ID when using Tag Manager’ is closed to new replies.