简体   繁体   English

如何使用JavaScript或PHP传回一个唯一的ID?

[英]How to pass back a unique ID with JavaScript or PHP?

I need to pass back a unique ID in place of "INSERT+ORDER+ID" for when a user clicks on our "Request Information" button to better track our visitors. 当用户点击我们的“请求信息”按钮以更好地跟踪访问者时,我需要传回一个唯一ID来代替“INSERT + ORDER + ID”。 Does anyone know how I can accomplish this? 有谁知道我怎么能做到这一点? Any help would be much appreciated, thanks! 任何帮助将不胜感激,谢谢!

<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>

assuming you mean a random unique ID, for the javascript event tracking this should work: 假设您的意思是随机唯一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);

About the content in the noscript tag, you can't do it with static html of course so you have to put in the context of your template (in the php file), something like this that generates a unique ID and then echo it in place of your placeholder. 关于noscript标签中的内容,你当然不能使用静态html,所以你必须放入模板的上下文(在php文件中), 这样生成一个唯一的ID然后在它中回显它占位符的位置。

Since I'm in the mood for structuring and cleaning, I took the liberty of refactoring a bit the code, in case, you can replace all your script with this (assuming you're using plain js (vanilla) and html 5): 由于我有结构化和清理的心情,我冒昧地重构了一些代码,万一你可以用这个替换你的所有脚本(假设你使用普通的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>

I would suggest using an AJAX call that is binded to the onclick event or .click if you're using JQuery 我建议使用绑定到onclick事件的AJAX调用或.click如果你使用的是JQuery

The AJAX call would hit a PHP script or call whatever you're using for analytics. AJAX调用将触及PHP脚本或调用您用于分析的任何内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM