Load Trade Desk Image Pixels Manually

Posted by: Ed Hungerford Date Posted: November 20, 2024
Back to Case Studies

Issue

The client wanted to install Trade Desk image pixels on their staging site, which didn’t have Google Tag Manager installed.

Solution

I wrote a script to load the base pixel on the staging site, and then to load the image pixels when the necessary conditions were met using Node.appendChild().

Achievement

Loaded Trade Desk image pixels using vanilla JavaScript instead of Google Tag Manager.

Snippet/Screenshot/Link

<?php //Load Trade Desk pixel on staging site only  ?>
<?php if($_SERVER['HTTP_HOST'] == "vilardifinancial.webpagefxstage.com") { ?>
    <script src="https://js.adsrvr.org/up_loader.1.1.0.js" type="text/javascript"></script>
    <script type="text/javascript">
        ttd_dom_ready(function () {
            if (typeof TTDUniversalPixelApi === 'function') {
                var universalPixelApi = new TTDUniversalPixelApi();
                universalPixelApi.init("2agn4le", ["m3qql81"], "https://insight.adsrvr.org/track/up", {
                    "td1": (function(name) { return ("; " + document.cookie).split("; " + name + "=").pop().split(";").shift(); })('__fx') || 'unidentified',
                    "td2": 36114
                });
            }
        });
    </script>
    <script>
        let ttdImagePixel3 = "https://insight.adsrvr.org/track/pxl/?adv=2agn4le&ct=0:txbz6ap&fmt=3&td3={td3}";
        let ttdImagePixel4 = "https://insight.adsrvr.org/track/pxl/?adv=2agn4le&ct=0:52jb701&fmt=3&td4={td4}";
        let ttdImagePixel5 = "https://insight.adsrvr.org/track/pxl/?adv=2agn4le&ct=0:8gdhl1u&fmt=3&td5={td5}";
        let el = document.createElement("img");
        el.height = "1px", el.width = "1px", el.style = "border-style:none;", el.alt = "";
        document.addEventListener("click", function(event){
            if(event.target.href && event.target.href.includes("tel:")) {
                el.src = ttdImagePixel3;
                document.head.appendChild(el);
            } else if (event.target.href && event.target.href == "https://form.jotform.com/231797206045053") {
                el.src = ttdImagePixel4;
                document.head.appendChild(el);
            } else if (event.target.matches("a[href='http://www.vilwm.com/'], a[href='http://www.vilwm.com/'] *")) {
                el.src = ttdImagePixel5;
                document.head.appendChild(el);
            }
        })
    </script>
<?php } ?>