[英]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.