Hi Sonny, try running it from a javascript action inside a nanoflow.
If you need it to run on pageload you can use the microflow timer widget for that.
Hi Sonny,
Did you get it to work? I'm following The data layer | Google Tag Manager for Web | Google Developers but I'm struggling to get the push event executed.
I have this on my index.html:
<script>
window.dataLayer = window.dataLayer || [];
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager →
In a nanoflow JS action I have an custom event, but I can't see it going out using the Network tab dev tools:
export async function JS_DatalayerPush(event) {
// BEGIN USER CODE
if (!event) {
return false;
}
dataLayer.push(event);
return true;
// END USER CODE
Can you see what I'm doing wrong?
[Update]
So with some additional info from Sonny I was able to get it working in a JS action within a nanoflow. I now have a action with some input parameters to fire away when needed. Althoug I couldn't see the data going through my network tab in DevTools, I was able to see the events coming in on Google Tag Assistant which is really helpfull when developing as usually the GA dashboard is not yet available.
export async function JS_Analytics_DatalayerPush(event, element, category, value) {
// BEGIN USER CODE
try {
var mxform = mx.ui.getContentForm().path;
var sessionData = mx.session.sessionData;
dataLayer.push({
"applicationTitle" : sessionData.uiconfig.profile.title,
"event" : event,
"category": category,
"element": element,
"value": value,
"userId" : mx.session.getUserId(),
"userRoles" : mx.session.getUserRoleNames(),
"pageName" : mxform.substr(0, mxform.length - ".page.xml".length),
"pageTitle" : mxform.title,
"sessionObjectId" : sessionData.sessionObjectId,
"sessionProfile" : sessionData.uiconfig.profile.name,
"sessionLocale" : sessionData.locale.code
});
console.info("Event pushed");
} catch(e) {
console.error(e.message);
}
// END USER CODE
}