Question

I am trying to pass the daysLeft variable to the Google Tag Manager Data Layer but it will not work. If I use use this;

 var newname = 'daysLeft';

It fires the tag but in the report it appears as daysLeft. If I try to fire the tag with

 var newname = daysLeft;

The tag does not work.

        <script type="text/javascript">
    var today = new Date(); // Get todays date.
    var bookedDate = new Date('June 29, 2014'); // Fill this with the date of the booking in this format - June 29, 2013.
    var msPerDay = 24 * 60 * 60 * 1000 ; // Work out the number of milliseconds in a day.
    var timeLeft = (bookedDate.getTime() - today.getTime()); // Booked date minus todays tate, returned in milliseconds.
    var e_daysLeft = timeLeft / msPerDay; // Divide millieseconds left untill booking by the number of milliseconds in a day.
    var daysLeft = Math.floor(e_daysLeft); // Round the total down to the nearest integer.

    var newname = daysLeft;

    dataLayer = [{'event' : 'newname' }];

    </script> <!-- Google Tag Manager -->
    <noscript><iframe height="0" src="//www.googletagmanager.com/ns.html?id="
    style="display:none;visibility:hidden" width="0"></iframe></noscript>
    <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=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','');
    </script> <!-- End Google Tag Manager -->
Was it helpful?

Solution

Worked it out, for some reason the dataLayer wouldn't pass an integer.

I had to convert it to a string first with

daysLeft.toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top