Tracking Multi-part steps with GTM data layer (dataLayer.push)

How to pass information to tags with the use of the data layer object. This allows you to pass events or variables. It also allows you to setup triggers based the values of variables.

Make sure to also read the Google Data Layer documentation if you haven't already:

The below JavaScript code is just an example, you will need to make changes accordingly based on your use case. Place this code in the footer of your site.

    function hashChanged(storedHash){
        // If hash contains step
            // When no hash starting with `#step-` was found we cancel
        // Track field value
        var brandFieldName = 'brand';
        var brandFieldValue = (document.querySelector('input[name="'+brandFieldName+'"') ? document.querySelector('input[name="'+brandFieldName+'"').value : 'Field not found!');
        // Grab current step number
        var step = '1', stepFormID, multiPart, step, explodedStep = currentStep.split('-');
            stepFormID = explodedStep[1];
            multiPart = explodedStep[2].split(';');
            step = multiPart[0];
        // Grab the current page including current multi-part step from the URL
        var path = location.pathname + + location.hash;
        if(typeof dataLayer === 'undefined') return;
            event: 'step_'+step+'_complete',
            brand: brandFieldValue,
            form_step: step,
            page_title: document.title, // e.g: `Page title`
            page_location: location.href, // e.g: ``
            page_path: path // e.g: `` (Form ID 12345 and currently at step 2)
    // Track multi-part
    if('onhashchange' in window) { // event supported?
        window.onhashchange = function () {
    }else{ // event not supported:
        var storedHash = window.location.hash;
        window.setInterval(function () {
                storedHash = window.location.hash;
        }, 100);

Last updated