Dynamically generating tracking images

Ruslan Sivak russ at vshift.com
Wed Oct 27 18:51:22 CEST 2010


We are building a site which needs to be cached, but we need to have a
tracking image that gets output for every page.  This image should contain
certain (lets say all) parameters of the query string.

I'm thinking of implementing this using ESI.  I currently have the
following:

test.php
 <esi:include src="/esi/tracker.pl"/>

/esi/tracker.pl
#!/usr/bin/perl
use CGI::Cookie ();
use Apache2::RequestRec ();
use APR::Table ();
my $cookie = CGI::Cookie->new(-name  => 'mod_perl',
                                    -value => 'awesome');
print "Set-Cookie: $cookie\n";
print "Content-type: text/html\n\n";
print <<ENDHTML;
$ENV{'QUERY_STRING'}
<div>
<img src="
http://sometrackingsite/id/12345.gif?url=ENTER+PAGE+URL+HERE&title=ENTER+PAGE+TITLE+HERE"
alt="" height="1" width="1" />
</div>
ENDHTML

default.vcl
...
sub vcl_fetch {
  set beresp.ttl=5d;
   if (req.url ~"test.php") {
        esi;  /* Do ESI processing */
        set beresp.ttl = 24 h;
    } elseif (req.url == "/esi/tracker.pl") {
        set beresp.ttl = 1m;
    }
  if (beresp.http.set-cookie) {
    set beresp.http.set-cookie = ";" beresp.http.set-cookie;
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "; +",
";");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie,
";(MY_COOKIE)=", "; \1=");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, ";[^
][^;]*", "");
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "^[;
]+|[; ]+$", "");

    if (beresp.http.set-cookie == "") {
        remove beresp.http.set-cookie;
    }
    return (deliver);
   }
}
...

Am I going about this the right way?  Perl doesn't seem to see the query
string or be able to set any cookies.

Any help would be appreciated.

Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20101027/daadae57/attachment-0003.html>


More information about the varnish-misc mailing list