How to use Tracking API script to track affiliate outbound clicks [With code sample]


#1

Sorry if this was answered somewhere else but I’ve searched and can’t seem to find the answer. I use a small php script to handle all the affiliate links on a website, this is the code I use:


<?php
     $id = $_GET['id'];
     $redirect = array(

'name1'=>'http://www.affiliatelink1.com/',
'name2'=>'http://www.affiliatelink2.com/',

           );
     header('HTTP/1.1 301 Moved Permanently');
     header('Location:'.$redirect[$id]);
     header('Connection: close');
     exit();
?>

Is there a way to track these outbound links as goals? Thanks for your time!

Charles


(Matthieu Aubry) #2

You could, using the PHP tracker from the Tracking API: Tracking API - Analytics Platform - Matomo


#3

Thanks Matt, I checked it out and after lots of trial and error I got it to work. I thought I would share the code as some of it was hard to find:

Creat a file named outbound.php and place this code in it:


<?php
// -- Start Piwik Tracking --
require_once "outbound-tracking.php";  /* The tracking file you upload - I renamed mine to something unique */
PiwikTracker::$URL = 'http://www.your-site.com/mystats/';  /* The URL specified in your site tracking code */
$piwikTracker = new PiwikTracker( $idSite = 1 );  /* The Site ID also found in your tracking code */
$piwikTracker->setTokenAuth( 'your-auth-code-here' );  /* The token_auth found on the API page of your CP - required to get users IP */
$piwikTracker->setVisitorId($piwikTracker->getVisitorId());  /* You need to add this so the user id isn't lost and thier tracking starts over */
$piwikTracker->setIp( $_SERVER['REMOTE_ADDR'] );  /* Log visitor IP if you like */
$piwikTracker->setUrl($url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );  /* Set the current URL */
$piwikTracker->doTrackPageView( $_GET['id'] );  /* Log the Visit */
// -- End Piwik Tracking --
?> 

<?php
     $id = $_GET['id'];
     $redirect = array(

'name1'=>'http://www.affiliatelink1.com/',
'name2'=>'http://www.affiliatelink2.com/',

           );
     header('HTTP/1.1 301 Moved Permanently');
     header('Location:'.$redirect[$id]);
     header('Connection: close');
     exit();
?>

The file is called like so : outbound.php?id=name1

Create a new Goal in your control panel triggered by “Visit a given URL” where the “contains” value is : outbound.php

That’s it, you’re redirect will show inline with the rest of thier visit. There may be an easier way but this is what worked for me :slight_smile:


(Matthieu Aubry) #4

Thanks for the code, I moved the topic to the “Tutorial forum”


(Pırlanta) #5

that’s a good tip, thank you.


#6

Thanks ctcdesign for the code.
Matt and ctcdesign, I added these 3 more lines:


$piwikTracker->setUrlReferrer($_SERVER['HTTP_REFERER']);
$piwikTracker->setBrowserLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$piwikTracker->setUserAgent($_SERVER['HTTP_USER_AGENT']);

Fernando Batista
http://fernandobatista.net


#7

Works fine,
but in my case the users having the notracking cookie are also being counted?
Is there a workaround???


(bamtel) #8

This has been working great until the latest update to version 1.11 and now it is adding this as a new unique visitor.

The only change I made to the code above was the following. It was working fine on Piwik version 1.10.

$piwikTracker->doTrackGoal($idGoal = 1);

instead of

$piwikTracker->doTrackPageView( $_GET[‘id’] ); /* Log the Visit */


(bamtel) #9

Any ideas what could be causing this?

Thanks


#10

Same problem here, has somebody an idea? I am using exactly the code provided in the third post.