Javascript 通过Google标记管理器将Ajax POST XMLHttpRequest发送到数据层

Javascript 通过Google标记管理器将Ajax POST XMLHttpRequest发送到数据层,javascript,jquery,ajax,xmlhttprequest,google-tag-manager,Javascript,Jquery,Ajax,Xmlhttprequest,Google Tag Manager,请帮帮我,因为我卡住了 首先,我是Ajax和javascript的新手 所以我有: 我的网站上有AJAX计算器表单 我想跟踪谷歌分析中的输入、下拉和选择字段及其值 我已经安装并实现了谷歌标签管理器 我已经创建了在DOM准备将输出推送到数据层时触发自定义html标记: <script> (function() { var xhrOpen = window.XMLHttpRequest.prototype.open; var xhrSend = window.XMLHttpRequest

请帮帮我,因为我卡住了

首先,我是Ajax和javascript的新手

所以我有:

  • 我的网站上有AJAX计算器表单
  • 我想跟踪谷歌分析中的输入、下拉和选择字段及其值
  • 我已经安装并实现了谷歌标签管理器
  • 我已经创建了在DOM准备将输出推送到数据层时触发自定义html标记:

    <script>
    (function() {
    var xhrOpen = window.XMLHttpRequest.prototype.open;
    var xhrSend = window.XMLHttpRequest.prototype.send;
    window.XMLHttpRequest.prototype.open = function() {
        this.method = arguments[0];
        this.url = arguments[1];
        return xhrOpen.apply(this, [].slice.call(arguments));
    };
    window.XMLHttpRequest.prototype.send = function() {
        var xhr = this;
        var xhrData = arguments[0];
        var intervalId = window.setInterval(function() {
            if(xhr.readyState != 4) {
                return;
            }
            dataLayer.push({
                'event': 'ajaxSuccess',
                'eventCategory': 'AJAX ' + xhr.method,
                'eventAction': xhr.url + (xhr.method == 'POST' && xhrData ? ';' + xhrData : ''),
                'eventLabel': xhr.responseText
            });
            clearInterval(intervalId);
        }, 1);
        return xhrSend.apply(this, [].slice.call(arguments));
    };
    })();
    </script>
    
  • 最后一个问题是: 如何使用字符串拆分URL中接收的数据?我知道我应该在GTM中创建新触发器并编辑代码。但是怎么做呢?JSON.parse?JSON.stringify?:
  • 我希望在输出中包含的内容:

     {
    
        event: 'ajaxSuccess',
    
        eventCategory: 'AJAX POST',
    
        eventAction:'http://mylocalhosting.local/calculator/ajax_statistic_track;property_value=20000&state=1&property_type=1&first_home_buyer=2&are_you_purchasing=2&url=http%3A%2F%2Fnew.sharewood.ru%2Fembed.html',
    
        eventLabel:'property_value=20000&state=1&property_type=1&first_home_buyer=2&are_you_purchasing=2&url=http%3A%2F%2Fnew.sharewood.ru%2Fembed.html'
    
    propertyValue: '20000'
    
    state: '1'
    
    propertyType: '1'
    
    firstHomeBuyer:  '2'     
            }
    

    因为您将数据作为url传递,所以可以让GTM处理解析。GTM有一个“url”类型的变量,它还可以返回url的一部分,包括由查询参数标识的值。首先,创建一个读取eventAction(url格式)的数据层变量。然后创建一个新的“URL”类型变量,将“Component type”设置为query key以提取查询变量,设置查询参数名称,并在“More settings”中选择数据层变量,将eventAction设置为“source”参数。返回“propertyValue”的值时应该是这样的(对url中的其他查询参数重复此操作):


    谢谢,但这些都不起作用,因为eventAction中的参数不在URL中。我不能包括屏幕截图,但您可以看到它是如何工作的:“eventAction”:xhr.url+(xhr.method==“POST”&&xhrData?;“+xhrData:”),我可能在您的问题中犯了一个错误。我注意到,实际上,参数是与URL分开传递的。您没有正确阅读答案。这是因为URL变量类型不要求其输入值位于浏览器地址栏中,它只要求它看起来像URL,您的eventCategory就是这样做的。正如您在附带的屏幕截图中看到的,您可以将输入值设置为任何变量。我找到了一个解决方案。GTM并没有从URL获取它,因为分隔符是“;”,然后我在“?”上的脚本中被替换了,它工作了。非常感谢。
     {
    
        event: 'ajaxSuccess',
    
        eventCategory: 'AJAX POST',
    
        eventAction:'http://mylocalhosting.local/calculator/ajax_statistic_track;property_value=20000&state=1&property_type=1&first_home_buyer=2&are_you_purchasing=2&url=http%3A%2F%2Fnew.sharewood.ru%2Fembed.html',
    
        eventLabel:'property_value=20000&state=1&property_type=1&first_home_buyer=2&are_you_purchasing=2&url=http%3A%2F%2Fnew.sharewood.ru%2Fembed.html'
    
    propertyValue: '20000'
    
    state: '1'
    
    propertyType: '1'
    
    firstHomeBuyer:  '2'     
            }