tmartin314
tmartin314

Reputation: 4171

Track impressions from image src tag

I need to include an image tag that references a script:

<img src="http://the.site./script.php"/>

But I'm not sure of how to make this invisible. Sort of behind the scenes to track impressions.

2 Questions:

  1. How do I properly layout the tag?

  2. What would script.php contain?

Upvotes: 2

Views: 4293

Answers (3)

PleaseStand
PleaseStand

Reputation: 32112

Place your img element at the end of the page.

<img src="http://the.site./script.php?foo=bar&baz=quux" width="1" height="1"/>
</body>

Then in PHP, you can track the hit in a database. I assume you already know how to do that; if not, you can find more information about accessing a database from PHP on the Internet.

The only thing left is to return the image that the browser is expecting. You can do so using this code, which returns a 1x1 pixel transparent GIF image:

<?php

// Tracking code goes here

// Output a 1x1 transparent GIF
header('Content-type: image/gif');
echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');

Upvotes: 10

Piskvor left the building
Piskvor left the building

Reputation: 92772

You could make it into a web bug - the script could return a 1x1 transparent GIF image (create a 1x1 transparent GIF and save it on your server):

<?php
header('Content-Type: image/gif');
// whatever tracking/data mining you want to do with the data, do that here.
readfile('your_1x1_file.gif');
exit;
?>

Note that this is the technical side of things, the legal part on web tracking etc. is up to you (and your company's legal dept.)

Upvotes: 1

Oli
Oli

Reputation: 2464

script.php would contain a code that updates a tracking database with a single hit. Then it outputs a small, empty image (1 pix width and 1 pix height transparent).

This way you will know the user has visited the page, while the user won't see the image.

Upvotes: 6

Related Questions