1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

loading img pixel from a js file

Discussion in 'HTML & JavaScript' started by Mutikasa, Oct 2, 2015.

  1. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    579
    Likes Received:
    207
    So, I have bunch of landing pages and I want to load tracking pixel on visit. I don't want to add pixel to every page, I would just like to have it in a .js file so I can load the script file in header, without calling function in page. How do I do this?
     
  2. delite737

    delite737 Registered Member

    Joined:
    Nov 1, 2013
    Messages:
    70
    Likes Received:
    11
    Im not sure what a tracking token is,after a quick google,you basically need the IMG embedded to the page,so that it does a HTTP Get which is tracked by the tracker provider,try this

    <script>
    var myPixel = document.createElement("img")
    myPixel.src="https://linkToIMAGEHERE"
    document.body.appendChild(myPixel)
    </script>
     
    Last edited: Oct 2, 2015
  3. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    579
    Likes Received:
    207
    yes i need to append img, but why are you append it to the <head>? It needs to be in the <body>
     
  4. davids355

    davids355 Jr. VIP Jr. VIP

    Joined:
    Apr 25, 2011
    Messages:
    10,396
    Likes Received:
    8,100
    Why image? I thought only reason to use image for tracking was for clients that did not support Js...?
     
  5. delite737

    delite737 Registered Member

    Joined:
    Nov 1, 2013
    Messages:
    70
    Likes Received:
    11
    Hi,yes it does needs to be in body,document.body.appendChild,I was just getting since it was a http get,and the body does not exist at that point of execution,I think it would still work

    if you have jquery in your page write this

    $(document).ready(function(){
    var myPixel = document.createElement("img")
    myPixel.src="https://linkToIMAGEHERE"
    document.body.appendChild(myPixel)
    })
     
  6. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    579
    Likes Received:
    207
    is this gonna work if I put it in a .js file and link it in the <head>?
     
  7. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    579
    Likes Received:
    207
    I am not sure, it's a standard practice. Do you have code for JS?
     
  8. davids355

    davids355 Jr. VIP Jr. VIP

    Joined:
    Apr 25, 2011
    Messages:
    10,396
    Likes Received:
    8,100
    No I don't have code sorry, was just interested.
     
  9. delite737

    delite737 Registered Member

    Joined:
    Nov 1, 2013
    Messages:
    70
    Likes Received:
    11
    put this in the html file to inject jquery,put the below in a JS file,inject the JS file,after the jquery injection
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    ===========================

    $(document).ready(function(){
    var myPixel = document.createElement("img")
    myPixel.src="https://linkToIMAGEHERE"
    document.body.appendChild(myPixel)
    })



    paste that exactly as above into the head,changing the link
     
    • Thanks Thanks x 1
    Last edited: Oct 2, 2015