简体   繁体   English

在触发之前,如何在下一页进一步更改内联脚本标签

[英]How do I change an inline script tag further down page before it fires

I have an ecommerce thank you page that is sending an ecommerce.js hit to Google Analytics 我有一个电子商务感谢页面,该页面正在向Google Analytics(分析)发送ecommerce.js点击

<script>
        try{ga('require', 'ecommerce', 'ecommerce.js');
        ga('ecommerce:addTransaction', {
            'id': '082918-074143-128'
            , 'affiliation': ''
            , 'revenue': '2.09'
            , 'shipping': '0.00'
            , 'tax': '0.17'
        });ga('ecommerce:addItem',{ 
            'id': '082918-074143-128'
            , 'sku': ''
            , 'name': 'Cane Creek 1-1/8-inch Headset Shim Spacer .25mm'
            , 'category': 'Bicycling Catalog > Components > Headsets'
            , 'price': '1.08'
            , 'quantity': '1'
        });

        ga('ecommerce:addItem',{ 
            'id': '082918-074143-128'
            , 'sku': ''
            , 'name': 'Race Face X-Type BB Outer Race Seal Gray'
            , 'category': 'Bicycling Catalog > Components > Other > Other'
            , 'price': '0.84'
            , 'quantity': '1'
        }); ga('ecommerce:send');}catch(err){}
</script> 

I want to alter this script before it sends the data to GA by removing the first and last lines, as well as changing the code by changing the string 'ga(' to 'ga1(' so that the code won't send data to GA. and so that I can instead send the enhanced ecommerce hit instead. 我想更改此脚本,然后再删除第一行和最后一行,并通过将字符串'ga('更改为'ga1('来更改代码,以使代码不会将数据发送至GA。,这样我就可以发送增强型电子商务匹配。

I have inserted the following script higher on the page so that it would edit the tag lower on the page, but it fails to edit the tag before it fires. 我在页面上方插入了以下脚本,以便它将标签编辑在页面下方,但是在触发之前无法编辑标签。

I tried this: 我尝试了这个:

<script>
  document.addEventListener("DOMContentLoaded", function(event) {

    function contains(selector, text) {
        var elements = document.querySelectorAll(selector);
        return [].filter.call(elements, function(element){
            return RegExp(text).test(element.textContent);
        });
    }
    function bsHit() {
        gaHit = contains('script', /try\{ga\(/g);
        var hit = gaHit[0].innerHTML;
        var tryer = "try{ga('require', 'ecommerce', 'ecommerce.js')\;";
        var hit = hit.replace(tryer, "");
        lastLine = "ga('ecommerce:send');}catch(err){}";
        hit = hit.replace(lastLine, "");
        hit = hit.replace(/ga\(/g,"ga1\(");
        console.log(hit);
    }

    bsHit();
  }
</script>

What do I need to / modify in my code to make it happen? 我需要/修改我的代码来实现它吗?

I appreciate your time and insights. 感谢您的宝贵时间和见解。

Try the code below. 试试下面的代码。 I don't fully understand what your 'contains' function is doing, so I can't vouch for that. 我不完全了解您的“包含”功能在做什么,所以我不能保证。 In any case, if you use the GA debugger chrome extension, you should be able to see if your code executes before the autogenerated code, and troubleshoot from there. 无论如何,如果您使用GA调试器Chrome扩展程序,则应该能够查看代码是否在自动生成的代码之前执行,并从那里进行故障排除。

 <script> (function() { //this wraps your sript in an IIFE, which will execute as soon as it's parsed function contains(selector, text) { var elements = document.querySelectorAll(selector); return [].filter.call(elements, function(element) { return RegExp(text).test(element.textContent); }); } function bsHit() { var gaHit = contains('script', /try\\{ga\\(/g); var hit = gaHit[0].innerHTML; hit = hit.replace("ga('ecommerce:send')", ""); gaHit[0].innerHTML = hit; //var hit is just a string - you need to rewrite the script element console.log("Replaced script content:\\n" + hit); //added string so it will show up on console even if 'hit' is null } bsHit(); })(); </script> 

Could you just stub the ga function with something else temporarily? 您可以暂时将ga函数与其他函数一起使用吗?

 window.ga1 = window.ga; window.ga = function(){}; setTimeout(function(){ window.ga = window.ga1; }, 3000); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在页面加载之前更改 HTML 中脚本标记中的变量? - How do I change a variable in a script tag in HTML before the page has loaded? 如何在a中正确转义内联Javascript <script> tag? - How do I properly escape inline Javascript in a <script> tag? 向下滚动网页时如何更改图片? - How do I change a picture while scrolling down a web page? 如何通过以下方式将 JSX 导入 HTML 页面<script> tag? - How do I import JSX to an HTML page via the <script> tag? 如何在使用 Google 优化工具的 A/B 实验的结束正文标记之前添加脚本标记? - How do I add a script tag before the closing body tag for an A/B experiment using Google Optimize? 如何使用输入标签更改 html 页面的字体大小 - How do I change Font size of the html page with an input tag 我在带有脚本标签的网页中使用processing.js。 如何更改脚本标签的高度? - I am using processing.js in a web page with script tag. How can I change the height of the script tag? 我怎么有一个<script> tag run before html has loaded - How do I have a <script> tag run before html has loaded 我的<script> is at the bottom of the page, but still runs first; how do I get the body to render before running the script? - My <script> is at the bottom of the page, but still runs first; how do I get the body to render before running the script? 在<a>代码触发前</a>更改其href - Changing the href of an <a> tag before it fires
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM