Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to build a pixel that would track the current URL the user is on when they visit. I can use either JS (preferred) or a 1x1 image pixel. With JS I am assuming that I'd need to run an AJAX request to a PHP script to capture the info that I need and with an image pixel I am having issues getting the currently URL.

I also thought about URL encoding the current URL with JS and dynamically placing the image pixel with the encoded current URL as a query string to a PHP script, but that I can get to be very long.

If I am to go the AJAX route, which AJAX library can I use? JQuery is too bloated for this purpose.

Any other ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can write a script that creates and returns a .gif, .jpeg or .png image using PHP for tracking purposes using the GD library (which is often distributed with PHP in modern versions). If you don't have access to GD, you can always recompile PHP with GD enabled.

Example:

pixel.php (commented for the purposes of explanation):

<?php

  // Create an image, 1x1 pixel in size
  $im=imagecreate(1,1);

  // Set the background colour
  $white=imagecolorallocate($im,255,255,255);

  // Allocate the background colour
  imagesetpixel($im,1,1,$white);

  // Set the image type
  header("content-type:image/jpg");

  // Create a JPEG file from the image
  imagejpeg($im);

  // Free memory associated with the image
  imagedestroy($im);

?>

In a simple example, you can then call this tracking pixel using the following example URL in an email or other page:

<img src="http://example.com/pixel.php?a=value1&b=value2&c=value3">



Using variables:

Within your pixel.php you can then parse and interpret any $_GET variables that are passed to it within the image tag, simplistically:

if (isset($_GET['a'])) {
  // (Do|log) act on a
}
if (isset($_GET['b'])) {
  // (Do|log) act on b
}
if (isset($_GET['c'])) {
  // (Do|log) act on c
}

Apply and repeat as you need, but you can be quite sophisticated about what you do and especially as you have access to quite a lot of information about the user through being able to set vars on the $_GET string.

A more applicable example might be:

<img src="http://example.com/pixel.php?userid=98798&campaign=302&last=8">



Tracking more than just $_GET variables:

You can also pick up much more information using PHP, such as:

// Server variables
$ip = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser(null, true);
etc...

and then perhaps insert into a tracking table in your database:

$sql = "INSERT INTO campaign_tracking 
        ('when','campaign','last','ip','useragent') 
        VALUES 
        (NOW(),'$campaign','$last','$ip','$useragent')";

This is a(the) basic method used widely for tracking email marketing campaigns and specifically in PHP, but the same method is applicable using other scripting/programming languages and libraries - and for other purposes too.

Further and useful information on GD:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...