Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Tag Manager数据层变量未定义_Javascript_Jquery_Ajax_Google Tag Manager_Google Datalayer - Fatal编程技术网

Javascript Google Tag Manager数据层变量未定义

Javascript Google Tag Manager数据层变量未定义,javascript,jquery,ajax,google-tag-manager,google-datalayer,Javascript,Jquery,Ajax,Google Tag Manager,Google Datalayer,在调用Ajax时,我使用JQuery推送数据层变量。我使用的完整脚本如下所示: (功能(){ “严格使用”; var$; var n=0; init(); 函数init(n){ //确保jQuery在执行任何操作之前可用 if(jQuery的类型!==“未定义”){ //在本地定义我们的$shortcut $=jQuery; bindToAjax(); //最多检查10秒钟 }否则,如果(n

在调用Ajax时,我使用JQuery推送数据层变量。我使用的完整脚本如下所示:

(功能(){

“严格使用”;
var$;
var n=0;
init();
函数init(n){
//确保jQuery在执行任何操作之前可用
if(jQuery的类型!==“未定义”){
//在本地定义我们的$shortcut
$=jQuery;
bindToAjax();
//最多检查10秒钟
}否则,如果(n<20){
n++;
setTimeout(init,500);
}
}
函数bindToAjax(){
$(document).bind('ajaxComplete',函数(evt、jqXhr、opts){
//创建一个伪a元素,用于神奇简单的URL解析
var fullUrl=document.createElement('a');
fullUrl.href=opts.url;
//IE9+从a.pathname中去掉了前导斜杠,因为谁想在周五准时回家
var pathname=fullUrl.pathname[0]==='/'?fullUrl.pathname:'/'+fullUrl.pathname;
//手动删除前导问号(如果有)
var queryString=fullUrl.search[0]==='?'?fullUrl.search.slice(1):fullUrl.search;
//将参数和标题转换为对象以便于参考
var queryParameters=objMap(queryString,,,,,,,,true);
var headers=objMap(jqXhr.getAllResponseHeaders(),'\n',':');
//盲目地推送到数据层,因为这会在GTM中触发
数据层推送({
“事件”:“ajaxComplete”,
“属性”:{
//返回空字符串以防止意外继承旧数据
“类型”:选择.type | |”,
“url”:fullUrl.href | |”,
“queryParameters”:queryParameters,
“路径名”:路径名| |”,
“主机名”:fullUrl.hostname | |”,
“协议”:fullUrl.protocol | |”,
“片段”:fullUrl.hash | |”,
“状态代码”:jqXhr.status | |”,
“statusText”:jqXhr.statusText | |”,
“头”:头,
“时间戳”:evt.timestamp | |”,
“contentType”:opts.contentType | |”,
//遵从jQuery对响应的处理
“响应”:(jqXhr.responseJSON | | jqXhr.responseXML | | | jqXhr.responseText | |”
}
});
});
}
函数objMap(数据、数据、spl、解码){
var obj={};
//如果缺少一个参数,则返回一个空对象
如果(!data | | |!delim | |!spl){
返回{};
}
var arr=数据分割(delim);
var i;
如果(arr){
对于(i=0;i
})()

当我在控制台中进行检查时,大部分情况下一切正常:

数据层图像

但当我在GTM中预览它时,它被认为是未定义的:

开始日期


有人能指导我,让正确的值显示出来吗?触发它的事件称为ajaxComplete。

如何配置变量?是否使用点符号来获取数据层变量的值?我在GTM中将数据层变量配置为attribute.startDate和startDate。还是使用queryParameters.startDate?我正在正确地将数据传递到数据层并设置多个点符号,但在GTM预览模式下查看时,它显示为未定义。
'use strict';
var $;
var n = 0;
init();

function init(n) {

  // Ensure jQuery is available before anything
  if (typeof jQuery !== 'undefined') {

    // Define our $ shortcut locally
    $ = jQuery;
    bindToAjax();

  // Check for up to 10 seconds
  } else if (n < 20) {

    n++;
    setTimeout(init, 500);

  }

}

function bindToAjax() {

  $(document).bind('ajaxComplete', function(evt, jqXhr, opts) {

    // Create a fake a element for magically simple URL parsing
    var fullUrl = document.createElement('a');
    fullUrl.href = opts.url;

    // IE9+ strips the leading slash from a.pathname because who wants to get home on time Friday anyways
    var pathname = fullUrl.pathname[0] === '/' ? fullUrl.pathname : '/' + fullUrl.pathname;
    // Manually remove the leading question mark, if there is one
    var queryString = fullUrl.search[0] === '?' ? fullUrl.search.slice(1) : fullUrl.search;
    // Turn our params and headers into objects for easier reference
    var queryParameters = objMap(queryString, '&', '=', true);
    var headers = objMap(jqXhr.getAllResponseHeaders(), '\n', ':');

    // Blindly push to the dataLayer because this fires within GTM
    dataLayer.push({
      'event': 'ajaxComplete',
      'attributes': {
        // Return empty strings to prevent accidental inheritance of old data
        'type': opts.type || '',
        'url': fullUrl.href || '',
        'queryParameters': queryParameters,
        'pathname': pathname || '',
        'hostname': fullUrl.hostname || '',
        'protocol': fullUrl.protocol || '',
        'fragment': fullUrl.hash || '',
        'statusCode': jqXhr.status || '',
        'statusText': jqXhr.statusText || '',
        'headers': headers,
        'timestamp': evt.timeStamp || '',
        'contentType': opts.contentType || '',
        // Defer to jQuery's handling of the response
        'response': (jqXhr.responseJSON || jqXhr.responseXML || jqXhr.responseText || '')
      }
    });

  });

}

function objMap(data, delim, spl, decode) {

  var obj = {};

  // If one of our parameters is missing, return an empty object
  if (!data || !delim || !spl) {

    return {};

  }

  var arr = data.split(delim);
  var i;

  if (arr) {

    for (i = 0; i < arr.length; i++) {

      // If the decode flag is present, URL decode the set
      var item = decode ? decodeURIComponent(arr[i]) : arr[i];
      var pair = item.split(spl);

      var key = trim_(pair[0]);
      var value = trim_(pair[1]);

      if (key && value) {

        obj[key] = value;

      }

    }

  }

  return obj;

}

// Basic .trim() polyfill
function trim_(str) {

  if (str) {

    return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');

  }

}