为什么变量未传递到Google跟踪代码管理器的数据层

时间:2013-06-25 08:07:59

标签: javascript google-analytics data-layers google-tag-manager

我正在尝试将daysLeft变量传递给Google跟踪代码管理器数据层,但它无法正常工作。如果我使用这个;

 var newname = 'daysLeft';

它会触发标记,但在报告中显示为daysLeft。如果我尝试使用

触发标记
 var newname = daysLeft;

标签不起作用。

        <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 -->

1 个答案:

答案 0 :(得分:1)

由于某种原因,dataLayer不会传递整数。

我必须先用

将其转换为字符串
daysLeft.toString();