How to create and implement a pixel tracking code
Asked Answered
B

4

16

OK, here's a goal I've been looking for a while.

As it's known, most advertising and analytics companies use a so called "pixel" code in order to track websites views, transactions, conversion etc.

I do have a general idea on how it works, the problem is how to implement it. The tracking codes consist from few parts.

  1. The tracking code itself. This is the code that the users inserts on his webpage in the <head> section. The main goal of this code is to set some customer specific variables and to call the *.js file.

  2. *.js file. This file holds all the magic of CRUD (create/read/update/delete) cookies, track user's events and interaction with the webpage.

  3. The pixel code. This is an <img> tag with the src atribute pointing to an image *.gif (for example) file that takes all the parameters collected on the page, and stores them in the database.

Example:

WordPress pixel code: <img id="wpstats" src="http://stats.wordpress.com/g.gif?host=www.hostname.com&amp;list_of_cookies_value_pairs;" alt="">

Google Analitycs: http://www.google-analytics.com/__utm.gif?utmwv=4&utmn=769876874&etc

Now, it's obvious that the *.gif request has to reach a server side scripting language in order to read the parameters data and store them in a db.

Does anyone have an idea how to implement this in Zend?

UPDATE Another thing I'm interested in is: How to avoid the user's browser to load the cached *.gif ? Will a random parameter value do the trick? Example: src="pixel.gif?nocache=random_number" where the nocache parameter value will be different on every request.

Britteny answered 1/2, 2013 at 15:10 Comment(2)
Hi, check this out on the chache problem. https://mcmap.net/q/359910/-developing-a-tracking-pixelKathline
One quick trick for the cache problem is to ask the browser not to cache the image.header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");Hammons
E
2

As Zend is built using PHP, it might be worth reading the following question and answer: Developing a tracking pixel.

In addition to this answer and as you're looking for a way of avoiding caching the tracking image, the easiest way of doing this is to append a unique/random string to it, which is generated at runtime.

For example, server-side and with the creation of each image, you might add a random URL id:

<?php

  // Generate random id of min/max length
  $rand_id = rand(8, 8);

  // Echo the image and append a random string
  echo "<img src='pixel.php?a=".$vara."&b=".$varb."&rand=".$rand_id."'>";

?>
Existentialism answered 6/5, 2013 at 0:5 Comment(0)
B
2

Just adding my 2 cents to this thread because I think an important, and frequently used, option is missing: you don't necessarily need a scripting language to capture the request. A more efficient approach is to use the web server access log (like apache access log for instance) to log the request and then handle that log with whatever tools you see fit, like ELK stack for instance.

This makes serving the requests much lighter because no scripting language is loaded to prepare the response, just native apache response, which is typically much more efficient.

Biscay answered 10/12, 2015 at 7:56 Comment(0)
T
0

First of all, the *.gif doesn't need to be that file type, the only thing that is of interest is the Content-Type http header. Set that to image/gif (or any other, appropiate type) in the beginning, execute your code and render some sort of image to the response body.

Tetchy answered 1/2, 2013 at 15:13 Comment(3)
Thank you, yes, I did create the proper headers and actually generating the pixel.gif file with PHP imagecreatefromgif() which actually has the php extension. The only thing is that, when www.customerpage.com loads <img src="mypage.com/pixel.gif?params"> ZF throws an error, that controller pixel.gif doesn't exist.Britteny
Have you added a route for it? And see also the answer on #3203854 - this will be much faster than using imagecreatefromgifContradance
about the route, not yet, I'm wondering if there's a way to do it without routing. And thank you for the link for the transparent pixel, it;s really useful.Britteny
A
0

Well, all of the above codes are correct and is good but to be certain, the guy above mention "g.gif"

You can just add a simple php code to write to an sql or fwrite("file.txt",$opened) where var $opened serves as the counter++ if someone opened your mail... then save it as "g.gif"

TO DO all of this just add these:

<Files "/thisdirectory">
 AddType application/x-httpd-php .gif
</Files>

to your ".htaccess" file but be sure to make a new directory for that g.gif or whatever.gif where the directory only contains g.gif and .htaccess

Adlare answered 24/9, 2014 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.