views:

1283

answers:

4

I understand the basics of pixel tracking, I'm able to insert a pixel that references an image on my tracking domain to other websites.

However, how do I increment that actual pixel count on my tracking domain? Is there some kind of log that tells me every time that pixel image was served up? Am i able to do something like:

<img src="http://www.foo.com/serveImage/getImage.php?id=5123" />

then have the getImage page, serve up the image, and increment based on the id that was passed in? or is there a better way to achieve this?

Thank you in advance.

+2  A: 

Yes you have the right idea.

You give each site or page a unique ID, which is then passed in the image src. So in your example the ID is 5123.

In your getImage.php script then use this ID to increment the database (or however you record the data) and return any image that you want to. If you want the image you return to show the number of hits you can create an image on the fly with the GD extention - see the PHP manual for more information on it.

Jacob Wyke
A: 

this is my track code:

<?php

$id = $_GET['site_id'];

// do track

$imageFile = 'images/pixel.jpg';
$im = imagecreatefromjpeg($imageFile);
header('Content-type: image/jpeg');
imagejpeg($im);


?>
Gabriel Sosa
If you want to reduce resource use, don't use GD to read the image file and then spit it back out. Just send the content header, and then use `readfile()`.
gnud
+1  A: 

if you want to just output a gif this is a quick simple way, just make sure your script doesn't output anything else before or after:

header("Content-type: image/gif");
header("Content-length: 43");
$fp = fopen("php://output","wb");
fwrite($fp,"GIF89a\x01\x00\x01\x00\x80\x00\x00\xFF\xFF",15);
fwrite($fp,"\xFF\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00",12);
fwrite($fp,"\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02",12);
fwrite($fp,"\x44\x01\x00\x3B",4);
fclose($fp);
nsbucky
A: 

Kind of a tangential answer, but too long for a comment:

You don't necessarily need to increment anything, depending on how you implement it. If you're aiming for making it super-fast, just relying on the server request logs should suffice. Every request for "getImage.php?q=5123" will already be there, you just need to pluck out the relevant info from the querystring.

You can parse the logs into a nice, query-able database later (via cron et al), out of band where it won't affect serving up the tracking bugs. Doing it all in one shot is a little more elegant, but if you're handling a lot of requests, the logs are already there anyway.

Bonus: server logs also have referrers & timestamps, so you can more easily see if someone is directly hammering getImage.php or linking to it from elsewhere to game the numbers, should those numbers be worth something.

tadamson