gpt4 book ai didi

javascript - 如何使用 JavaScript 或 PHP 传回唯一 ID?

转载 作者:行者123 更新时间:2023-12-02 17:45:47 24 4
gpt4 key购买 nike

当用户点击我们的“请求信息”按钮时,我需要传回一个唯一的 ID 来代替“INSERT+ORDER+ID”,以更好地跟踪我们的访问者。有谁知道我怎样才能做到这一点?任何帮助将不胜感激,谢谢!

<script type="text/javascript"> 
var _qevents = _qevents || [];

(function() {
var elem = document.createElement('script');
elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") +
".quantserve.com/quant.js";

elem.async = true;
elem.type = "text/javascript";
var scpt = document.getElementsByTagName('script')[0];
scpt.parentNode.insertBefore(elem, scpt);
})();

_qevents.push(
{qacct:"p-yeADJca0S9FXE",labels:"_fp.event.Request Information Confirmation
Page",orderid:"INSERT+ORDER+ID"}
);
</script>
<noscript>
<img src="//pixel.quantserve.com/pixel
/p-yeADJca0S9FXE.gif?labels=_fp.event.Request+Information+Confirmation+Page&
orderid=INSERT+ORDER+ID" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/>
</noscript>

最佳答案

假设您指的是随机的唯一 ID,对于 javascript 事件跟踪,这应该有效:

// function to generate random id in js
function getRandomId() {
var id = +new Date() + Math.random();
return id.toString().replace('.','');
}
var btnReqInfo = document.getElementById('request_information_btn');
// bind the click on button
btnReqInfo.addEventListener('click', function() {
// track the event
_qevents.push({ qacct:"p-yeADJca0S9FXE", labels: "_fp.event.Request Information ConfirmationPage",orderid: getRandomId() });
, false);

关于 noscript 标签中的内容,您当然不能使用静态 html 来做到这一点,因此您必须将其放入模板的上下文中(在 php 文件中),something like this that generates a unique ID然后回显它来代替占位符。

由于我有兴趣进行结构化和清理,所以我冒昧地重构了一些代码,以防万一,您可以用它替换所有脚本(假设您使用的是纯 js(vanilla)和 html 5):

<script> 
var _qevents = _qevents || [];

(function() {
var
init = function() {
loadScript();
bindUi();
},
loadScript = function() {
var elem = document.createElement('script');
elem.src = (document.location.protocol == "https:" ? "https://secure" : "http://edge") + ".quantserve.com/quant.js";
elem.async = true;
elem.type = "text/javascript";
var scrpt = document.getElementsByTagName('script')[0];
scrpt.parentNode.insertBefore(elem, scrpt);
},
bindUi = function() {
var btnReqInfo = document.getElementById('request_information_btn');
btnReqInfo.addEventListener('click', track.order, false);
},
track = {
order: function() {
_qevents.push({ qacct:"p-yeADJca0S9FXE", labels: "_fp.event.Request Information ConfirmationPage", orderid: utils.getRandomId() });
}
},
utils = {
getRandomId : function() {
var id = +new Date() + Math.random();
return id.toString().replace('.','');
}
};
init();
})();
</script>
<小时/>

关于javascript - 如何使用 JavaScript 或 PHP 传回唯一 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com