Question

In Piwik, campaign URLs look something like this: http://example.org/landing.html?pk_campaign=Email-Nov2011

For a Drupal 6 site, I would like to use the campaign feature but at the same time avoid that URLs with the pk_campaign parameter spread to much.

My first thought was a header redirect through .htaccess, but that makes no sense since that would be before the Piwik tracking code is called. So it would need to be something like:

  1. Call Piwik tracking code
  2. Redirect to the URL without the pk_campaign parameter

I am wondering if there is a way to achieve this. Any thoughts?

Was it helpful?

Solution

One solution I came up with is using a javascript redirect right after the tracking code is displayed, but just in case the parameter pk_campaign is present. So I put the tracking code into the <head> section of the page and right after that:

<script type="text/javascript">
  if (location.search.indexOf('pk_campaign=')>=0)
  {
    document.location.href=location.protocol + '//' + location.host + location.pathname;
  }
</script>

That seems to work but is slower than a redirect through .htaccess oder PHP. Additionally it forces me to place the tracking code in the <head> and load it before the main content of the page.

Another option might be in PHP and htaccess. For example first redirecting to a count script (count.php) in case the URL contains the parameter pk_campaign (in .htaccess), something like this:

  RewriteCond %{QUERY_STRING} pk_campaign
  RewriteRule ^ /count.php?url=http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Then in count.php something like the following using PHP Output Control functions to avoid header errors:

<?php
  ob_start( );
?>

  <!-- Piwik tracking code here -->

<?php
  if (isset($_GET['pk_campaign'])) {
    ob_end_clean( );
    header( 'Location: ' . preg_replace('/\?.*$/', '', $_SERVER["REQUEST_URI"]) );
    exit;
  }

  ob_end_flush( );
?>

I haven't really tried that, though. Somehow we'd have to "tell" Piwik the URL of the originating page, not the one the tracking code is in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top