Add Google analytics Data layer Push

0
Hi, Is there a possibility to add a Google Analytics Data layer push event? For example: window.dataLayer = window.dataLayer || []; window.dataLayer.push({ originalLocation: document.location.protocol + '//' + document.location.hostname + document.location.pathname + document.location.search }); I tried it in a html snippet but it didnt worked. When i add this code in the Console of Chrome browser it will push the data correctly. 
asked
2 answers
1

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.

answered
0

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
}

 

answered