I have a website and setup GA4 with Google Tag Manager setup. In the GTM, I created a trigger and an event to get recorded in the GA4. I can see that event in my analytics dashboard.
I want to get the name of the event in my front-end whenever an event is triggered. I need it on my front-end to keep count of event history, to further process them. Is there anyway to get the name of the event?
Please let me know how can this be achieved.
What I tried so far:
- Create a new tag with all the triggers, which has a custom HTML
<script>
var eventHistory = JSON.parse(localStorage.getItem("eventHistory"));
if(eventHistory == null) eventHistory = [];
eventHistory.push(dataLayer.slice(-1)[0].event);
localStorage.setItem("eventHistory", JSON.stringify(eventHistory));
console.log(dataLayer.slice(-1)[0].event);
</script>
With this it is only storing gtm.click or gtm.scrollDepth or gtm.dom events, but not the actual event names I set in the tag manager.
I was expecting: the actual event names like add_to_cart, checkout and event names like that.