This is a simple code which I think is a good start.
The scope:
When a visitor clicks a link with a clear message in its anchor we consider this as a confirmation for his interest in
a specific product. We define keywords for products/groups and save them with the help of onclick() handlers as a Piwik custom variable. While the visitor clicks our ‘keyword tagged’ links we form an easy to handle and always available string which contains all his interests. This info can be used to display interest targeted advertisement.
// Basic things we need:
$token_auth = MY_PIWIK_AUTH_CODE;
$pageurl = "http://". $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; // Current page URL
$userlikes_cv_id = 1; // CustomVariable Index ID you want to use (1 to 5)
$userlikes_cv_name = 'UserLikes'; // Corresponding CustomVariable name (generic)
// Get Piwik visitor ID from cookie:
// Piwik cookie name syntax for current site: _pk_id.SITE_ID.XXXX
// Replace dots in cookie name with underscores, so "_pk_id.1.74c7" is "_pk_id_1_74c7"
list($visitorId) = explode('.',$_COOKIE['_pk_id_1_74c7']);
if( isset( $visitorId ) ){
$requrl = "http://mydomain.com/piwik/?module=API&method=Live.getLastVisitsDetails&idSite=1&format=PHP&segment=visitorId==$visitorId&token_auth=$token_auth&filter_limit=1";
$ay_req = unserialize(file_get_contents($requrl));
// What are the visitors interests until now?
// We need array_filter to remove NULL/EMPTY element after explode() (user interests still unknown)
$userlikes_history = array_filter(explode('|',$ay_req[0]['customVariables'][$userlikes_cv_id]['customVariableValue'.$userlikes_cv_id])); // Visitors known interests
With the collected elements of array $userlikes_history we can now display targeted ads.
function($userlikes_history){
if(in_array('widgets1',$userlikes_history)){echo 'Widget1 Irresistible Offer';}
}
But first we need to collect visitors interests. Prepare parameters for the onclick() handler:
$userlikes_history_update = $userlikes_history; // Clone visitors known interests
$userlikes_history_update[] = 'widgets1'; // Add visitors (possibly!) new interests (generic, adapt it to current page or product group or even single links)
$userlikes_history_update = implode('|',array_unique($userlikes_history_update)); // Final updated string
}
Now the links with the onclick() handler that helps us save the visitors interests. Note, for outlinks we dont need to call trackLink().
<a href="http://widget1-linkout.com" onclick="piwikTracker.setCustomVariable(<?php echo $userlikes_cv_id; ?>,'<?php echo $userlikes_cv_name; ?>','<?php echo $userlikes_history_update; ?>','visit');">Widget1 (Outlink)</a><br>
<a href="http://mydomain.com/info-widget1.php" onclick="piwikTracker.setCustomVariable(<?php echo $userlikes_cv_id; ?>,'<?php echo $userlikes_cv_name; ?>','<?php echo $userlikes_history_update; ?>','visit');piwikTracker.trackLink('<?php echo $pageurl; ?>', 'link');">Widget1 (Internal)</a>
Admins, feel free to change/simplify the code.