【问题标题】:Form Abandonment data google tag manager表单放弃数据谷歌标签管理器
【发布时间】:2016-01-05 10:38:08
【问题描述】:

用于 Google 跟踪代码管理器中的表单放弃跟踪。下面是代码(来自http://www.simoahava.com/analytics/track-form-abandonment-with-google-tag-manager/的原始代码)。我已经按照我的要求修改了代码。

    (function() {
    var formSelector = '.webform-component-fieldset';
    var attribute = 'name';
    var history = [];
    window.addEventListener('beforeunload', function() {
        if (history.length) {
            var x = history.join(' > ');
            var x= x.replace(/submitted[fieldset_user_info]/g,"");
            window.dataLayer.push({
                'event' : 'formAbandonment',
                'category' : 'Form Abandonment',
                'action' : x

            });
        };
    });
});

 document.querySelector(formSelector).addEventListener('change', function(e) {
    history.push(e['target'].getAttribute(attribute));
});
})();

代码正在执行,但输出不是我们想要的。 我为 eventAction 数据层 ('action': x) 得到的输出如下

提交[fieldset_user_info][first_name] > 已提交[fieldset_user_info][street_address_1] > 已提交[fieldset_user_info][street_address_2] > 已提交[fieldset_user_info][city] > 提交[fieldset_user_info][状态] > 已提交[fieldset_user_info][zip_code] > 已提交[fieldset_user_info] [e_mail_address] > 已提交[fieldset_user_info][confirm_e_mail_address] > 已提交[fieldset_user_info][last_name] > 已提交[fieldset_user_info] [street_address_2] > 已提交[fieldset_user_info][i_prefer_to_receive_communications_from_pfizer_by]

我想从输出中删除每次出现的文本“submitted[fieldset_user_info]”,但使用上面的代码我无法做到

在修改代码方面需要帮助吗?

【问题讨论】:

    标签: javascript jquery google-analytics google-tag-manager


    【解决方案1】:

    尝试替换字符串

    var x= x.replace(/submitted[fieldset_user_info]/g,"");
    

    按字符串:

    var x= x.replace("submitted[fieldset_user_info]","");

    根据description of JS replace() method,它支持正则表达式以及字符串作为第一个参数。

    我尝试在控制台中使用两种变体测试“replace()”方法:使用您的 RegEx 和使用简单字符串:方法仅使用字符串成功。可能是你的正则表达式有问题(在我看来它看起来是正确的)。

    【讨论】:

    • 嗨 Buttersmai 感谢您的帮助。我已经按照建议进行了更改,但问题是它只是将第一个“submitted[fieldset_user_info]”替换为“submitted[fieldset_user_info]”的其余部分可见。我想删除报告中的所有“submitted[fieldset_user_info]”
    • 刚刚在特殊字符 [ 之前添加了 \ 它对我有用
    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多