loading img pixel from a js file

Mutikasa

Power Member
Joined
May 23, 2011
Messages
589
Reaction score
216
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?
 
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:
yes i need to append img, but why are you append it to the <head>? It needs to be in the <body>
 
Why image? I thought only reason to use image for tracking was for clients that did not support Js...?
 
yes i need to append img, but why are you append it to the <head>? It needs to be in the <body>

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

is this gonna work if I put it in a .js file and link it in the <head>?
 
Why image? I thought only reason to use image for tracking was for clients that did not support Js...?
I am not sure, it's a standard practice. Do you have code for JS?
 
No I don't have code sorry, was just interested.
 
is this gonna work if I put it in a .js file and link it in the <head>?
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
 
Last edited:
Back
Top