How to use Pixel Tracking across domains in PHP
Asked Answered
P

4

6

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.

Polytechnic answered 23/7, 2009 at 18:0 Comment(0)
D
13

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);
Deneendenegation answered 23/6, 2010 at 17:18 Comment(1)
This outputs the image directly to the buffer without loading a file first. This is faster.Lyautey
B
3

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.

Bing answered 24/7, 2009 at 0:13 Comment(0)
I
3

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


?>
Izak answered 24/7, 2009 at 2:6 Comment(2)
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().Eliseo
@Eliseo you are right... now it will be more wise to redirect to the image with a 301 HTTP header...Izak
C
1

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.

Caudell answered 9/8, 2010 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.