Disclaimer: I am not a professional programmer. There may be better ways to go about this, but the following solution has worked for me.
Piwik 1.3 and up now provides the ability to force the visitorID and attributionInfo when using the tracking API. This is particularly exciting for those of us using Paypal as the payment gateway for an ecommerce site. Google Analytics doesn’t play nice with Paypal - their “solution” for ecommerce tracking is to place their tracking code in your paypal landing page, turn on auto-return and PDT. Unfortunately, most people don’t bother to continue to your landing page after completing their purchase on Paypal’s site. Which is why IPN is the only reliable way to get payment info from paypal to your site’s backend processes. Since javascript will not be executed in an IPN script, Piwik’s php tracking API now comes to the rescue!
Implementing e-commerce tracking for paypal users is a two step process:
Step 1: Modify the Piwik javascript tracking code on your order page. This is the last page on your site before you send users to paypal. We are going to retrieve the visitorID and attributionInfo from Piwik, and put it into paypal’s custom variable for later use.
1a: modify your paypal buy now form as follows:
- If you haven’t already, give the form a name.
1b: In my scenario, I was already using the custom var. So, I need to pass the php var I was using for this to javascript, where I will append the piwik information to it in step 1c.
<?php
// my existing code... then...
// Pass $custom to javascript (for piwik tracking later)
?>
<script type="text/javascript">
jsCustom = "<?php echo $custom; ?>";
</script>
<?php
//... the rest of my existing php code...
1c: Modify the standard Piwik JS tracking code to also grab the visitorID, etc. from Piwik, and forward it via paypal.
<!-- Piwik tracker customized for this page only -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://mydomain.com/piwik/" : "http://mydomain.com/piwik/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
[b]visitorId = piwikTracker.getVisitorId();
attributionInfo = JSON2.stringify(piwikTracker.getAttributionInfo());
jsCustom = jsCustom + "&" + visitorId + "&" + attributionInfo;[/b]
document.ppbuynow.custom.value = jsCustom;
} catch( err ) {}
</script><noscript><p><img src="http://mydomain.com/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
Notice that I’ve taken my existing data that I was already passing via Paypal’s custom var, and appended the piwik data, using ampersands as a delimiter.
Next post will show Step 2: modifying your IPN script.