I am trying to send and track GA4 event with below snippet. I cannot see in GA4 dashboard even in Debug View
. I can reach the dataLayer and see the event in that array. When I debuged with Google Analytics Debugger I saw this log which is not very clear. Does anybody know how to not 'abort' process and track events?
PS: this code runs in other websites I also thought cross-domain issue but found nothing.
(function () {
const head = document.getElementsByTagName('head')[0];
const myScript = document.createElement('script');
myScript.setAttribute(
'src',
`https://www.googletagmanager.com/gtag/js?id=${analyticsId}`
);
myScript.onload = function () {
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', analyticsId, { debug_mode: true });
const eventMethod = window.addEventListener
? 'addEventListener'
: 'attachEvent';
const eventer = window[eventMethod];
const messageEvent =
eventMethod === 'attachEvent' ? 'onmessage' : 'message';
eventer(messageEvent, (e) => {
if (e.data === 'condition1') {
gtag('event', 'event1', {
event_category: 'event2',
event_label: title,
send_to: analyticsId,
});
} else if (e.data === 'condition2') {
gtag('event', 'event2', {
event_category: 'event2',
event_label: title,
send_to: analyticsId,
});
}
});
};
head.insertBefore(myScript, head.children[1]);
})();
You need to set state 'cookie_flags': 'SameSite=None;Secure' and 'cookie_update': false on config.
gtag('set', {
'cookie_flags': 'SameSite=None;Secure'
});
gtag('config', analyticsId, {
'cookie_update': false
});
Reference: https://developers.google.com/analytics/devguides/collection/gtagjs/cookies-user-id#cookie_update