I'm tring to use google analytics to monitor activities on my laravel site, as you can see i managed to record the events by $user_name
witch is great however how to i associate this user name with another variable for example $project_id
so i can see data for each user and on each different project.
any idea how i can achieve this? here is my code:
<script>
$(document).ready(function(){
$(document).mousemove(function(){
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-9YXX', {
'custom_map': {'dimension<Index>': 'user_id'}
});
gtag('event', 'user_name', {'user_id': '{!!$user_name!!}'});
});
});
</script>
You're calling the gtag directly from your codebase. This will inevitably increase your technical debt as well as clutter your code. It's better conducted directly from GTM.
Also, you're mixing up things... Events are supposed to represent certain actions or states. So the idea is to generate events on key activities and then fill them with data you need to pass. You do it differently: you generate an event whenever you need to pass data, so now your events are pretty much meaningless, you only use them as containers for dimensions. That will be a problem later on.
Another issue there is... are you sending an event on every mousemove? Just to indicate user id? There are these things in GA4 called user-level dimensions/properties. You should look at them and set them with pageviews rather than wasting events and cluttering your analytics and your users' network traffic.
Now finally, we've got to your question. You can send several properties with your events. You can see that the last argument of your gtag() call is an object. Well, use it:
gtag('event', 'user_name', {
'user_id': '{!!$user_name!!}',
'project_id': '{!!$project_id!!}'
});
It's odd it didn't occur to you to try it.