开发跟踪像素

时间:2012-10-26 01:37:00

标签: php javascript pixel

我正在尝试构建一个像素,用于跟踪用户访问时当前的URL。我可以使用JS(首选)或1x1图像像素。使用JS我假设我需要向PHP脚本运行AJAX请求以捕获我需要的信息,并且使用图像像素我在获取当前URL时遇到问题。

我还考虑过使用JS编码当前URL的URL,并将带有编码的当前URL的图像像素作为查询字符串动态放置到PHP脚本中,但我可以使用很长时间。

如果我要使用AJAX路线,我可以使用哪个AJAX库?为此目的,JQuery过于臃肿。

还有其他想法吗?

6 个答案:

答案 0 :(得分:57)

您可以编写一个脚本,使用PHP创建并返回.gif.jpeg.png图像,以便使用GD library进行跟踪(通常随PHP一起发布)在现代版本)。如果您无法访问GD,则始终可以在启用GD的情况下重新编译PHP。

示例:

pixel.php(为解释的目的而评论):

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

?>

在一个简单示例中,您可以使用电子邮件或其他页面中的以下示例网址调用此跟踪像素:

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



使用变量:

pixel.php中,您可以解析并解释在图片代码中传递给它的所有$_GET变量,简单地说:

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
}

根据需要应用并重复,但您可以对自己所做的事情非常了解,尤其是通过能够在$_GET字符串上设置变量来访问有关用户的大量信息。

更适用的例子可能是:

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



跟踪的不只是$ _GET变量:

您还可以使用PHP获取更多信息,例如:

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

然后可能插入数据库中的跟踪表:

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

这是一种广泛用于跟踪电子邮件营销活动的基本方法,特别是在PHP中,但使用其他脚本/编程语言和库也可以使用相同的方法 - 以及其他用途。

有关GD的更多有用信息:

答案 1 :(得分:47)

这是跟踪像素的另一个PHP实现,来自Open Web Analytics项目,它试图基本上是Google Analytics的PHP克隆。

它返回1x1 透明 GIF图像(不使用PHP图像库!),带有无缓存标头(对于精确跟踪很重要),并刷新输出以便您可以继续处理分析而不阻止HTTP响应(性能)。这似乎是一个非常先进的实现,值得尝试。

<?php
ignore_user_abort(true);

// turn off gzip compression
if ( function_exists( 'apache_setenv' ) ) {
  apache_setenv( 'no-gzip', 1 );
}

ini_set('zlib.output_compression', 0);

// turn on output buffering if necessary
if (ob_get_level() == 0) {
  ob_start();
}

// removing any content encoding like gzip etc.
header('Content-encoding: none', true);

//check to ses if request is a POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // the GIF should not be POSTed to, so do nothing...
  echo ' ';
} else {
  // return 1x1 pixel transparent gif
  header("Content-type: image/gif");
  // needed to avoid cache time on browser side
  header("Content-Length: 42");
  header("Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
  header("Expires: Wed, 11 Jan 2000 12:59:00 GMT");
  header("Last-Modified: Wed, 11 Jan 2006 12:59:00 GMT");
  header("Pragma: no-cache");

  echo sprintf('%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%',71,73,70,56,57,97,1,0,1,0,128,255,0,192,192,192,0,0,0,33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59);    
}

// flush all output buffers. No reason to make the user wait for OWA.
ob_flush();
flush();
ob_end_flush();

// DO ANALYTICS TRACKING HERE

答案 2 :(得分:10)

以这种方式输出1px x 1px:

header('Content-type: image/png');
echo gzinflate(base64_decode('6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfsEMYJC3jH0ikOLxdHEMqZiTnJCQAOSxMDB+E7cIBcl7uvq5rHNKaAIA'));

答案 3 :(得分:3)

这是一个用PHP编写的非常简化的跟踪像素。

How a Tracking Pixel Works

跟踪像素就像最原始的信标一样,它通过利用网页的事实来运作:图像是来自页面的单独请求。

如果您已经能够在其他人的页面上运行JS代码,那么您应该将数据发布回服务器。无需显示只能获得相同类型数据的微小像素。

答案 4 :(得分:0)

与这种效果类似的问题,因为在像素的高度中引入了对执行标记的功能的调用,该标记用于查看或打开电子邮件。

>
<img src="https://datafeeds.baruwa.com/1x1spacer.gif" width="1" height="1" alt="Web Bug from https://devorpenguin.des1.net/module/cartabandonmentpro/FrontCartAbandonment?token_cart=87c83b8f77318a54fdd6be91aacc3574&amp;id_cart=1002&amp;action=visualize&amp;wichRemind=1">

public static function visualize()
{

    $wichRemind = Tools::getValue('wichRemind');
    $id_cart = Tools::getValue('id_cart');
    $token = Tools::getValue('token_cart');

    if ($token == md5(_COOKIE_KEY_.'recover_cart_'.$id_cart)) {
        $query = "UPDATE "._DB_PREFIX_."cartabandonment_remind SET visualize = 1 WHERE wich_remind = ".(int)$wichRemind." AND id_cart = ".(int)$id_cart;
        Db::getInstance()->Execute($query);
    }

    header('Content-Type: image/png');
    echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=');

}

答案 5 :(得分:0)

如果您的项目范围需要使用OpenPixel,则可以解决大部分繁重的工作。