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.