0

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. 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>
June
  • 141
  • 2
  • 4
  • 9
  • Do you mean like generate a GUID? if so look at this answer http://stackoverflow.com/a/105074/2564103 – Finbarr Feb 13 '14 at 21:55

2 Answers2

2

assuming you mean a random unique ID, for the javascript event tracking this should work:

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

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

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

kuus
  • 453
  • 1
  • 5
  • 15
0

I would suggest using an AJAX call that is binded to the onclick event or .click if you're using JQuery

The AJAX call would hit a PHP script or call whatever you're using for analytics.

Newbi3
  • 356
  • 1
  • 8