问题
I need a custom event ("CasaCalculatorEvent") when clicking a modal. But in dataLayer, when I click its changes from "CasaCalculatorEvent" to "gtm.linkClick".
Receive like this in dataLayer:
My code:
回答1:
try this:
<button onclick="logThis()">
click me
</button>
var dataLayer = window.dataLayer || [];
function logThis(){
dataLayer.push({'event':'CasaCalculatorEvent'});
// print the last dataLayer entry
alert(JSON.stringify(dataLayer[dataLayer.length - 1]));
}
Fiddle: https://jsfiddle.net/ajsn/z4q93tya/
To get that working I needed to change the load settings to "no-wrap - bottom of body"
As Raymon pointed out, my preference would also be to add an id or distinguishing attribute to the element, then target it using GTM CSS Selectors to fire the event.
Instead of onclick="function()" add something like gtmevent="CasaCalculator", then use click event listeners on "some clicks" where target matches css selector [gtmevent="CasaCalculator"]
Simo-Ahava has a great post on css selectors: https://www.simoahava.com/analytics/css-selector-guide-google-tag-manager/
回答2:
Instead of hardcoding on the page, I would suggest setting up a "custom event" trigger on GTM with your eventName. The trigger can fire on your tag.
Edit1: Adding more info to clarify. I would suggest removing eventName from your code. Your code is fine without the event name. Then go on tagmanager.google.com and create a tag that references CasaCalculatorEvent (match the category, action, and label). Then create a trigger (this is where you will add the event name), that fires on the tag.
来源:https://stackoverflow.com/questions/59491696/custom-gtm-event-is-not-showing