setCustomVariable() fails on internal links?

When calling setCustomVariable() on links to another domain the name/value pair is saved.

<a href="http://www.OTHERDOMAIN.com/" onclick='piwikTracker.setCustomVariable ( 1, "NAME","VALUE","visit" ) '>Outbound Link</a>

… but fails on internal links:

<a href="http://www.MYDOMAIN.com/" onclick='piwikTracker.setCustomVariable ( 1, "NAME","VALUE","visit" ) '>Internal Link</a>

Why is this function restricted to outbound links? What can I do to get it for both cases?

The function is not restricted to internal links as you can see: http://dev.piwik.org/trac/browser/trunk/js/piwik.js#L2054

Hi Matt, the function is not the problem, yes. Could it be the way I intend to us it - in combination with “onclick”?

Can you clarify what is it exactly your trying to do and what’s not working?

What I try to do is establishing a quick overview of clicked links by visit to display interests-targeted ads. The site I currently build contains multiple hub pages, each lists similar products.
I could fire “setCustomVariable” in the classic way: right before “trackPageView”:

piwikTracker.setCustomVariable ( 1, “UserInterest”,“widgets1”,“visit” )

I now know that the visit has loaded the hub page “widgets1”.

But is he really interested in these products? To confirm his interest the visit would follow (=click) a link on the widgets1 page. Links on such a hub page are mixed: internal and outlinks. During a session a visit could end up with:

piwikTracker.setCustomVariable ( 1, “UserInterest”,“widgets1|widgets3|widgets4|widgets9”,“visit” )

And here I come to my problem: Calling setCustomVariable with onclick() saves the updated “UserInterest” if it is a outlink the visit clicks but not if the requested page is part of my own site (=visit clicks a internal link).

I know, I could get all info by looping through the visits actions but it would be better (=faster/efficient) to have everything saved as one single string (widgets1|widgets3…). Such a string is a valuable help to display interest-targeted advertisement without having to filter the visits actions on each page load.

Does setCustomVariable refuse to work on internal links in combination with onclick()? Any confirmation for yes or no?
In the meantime I will review my code once again.

I presume you’re adding an onclick event handler to call setCustomVariable(). The order that event listeners are called is browser-dependent, but it’s likely that your click handler is being called last.

What you should do is set the class attribute on that link to “piwik_ignore”. Then in your click handler call setCustomVariable() and trackLink() explicitly.

If you make it work for your use case, please suggest a new FAQ or documentation edit so we can make all users benefit from this :slight_smile:

Calling trackLink() did the trick.

This event handler works for Piwik custom variable on internal clicks:

onclick="piwikTracker.setCustomVariable( 1, "NAME", "VALUE", "visit" ); piwikTracker.trackLink('http://www.MYDOMAIN.com/currentpage.html', 'link');"

It also works without the “piwik_ignore” class.

I´ll do some more testing to see how practicable it is to apply this code in a production environment.

vipsoft, thanks for that hint.

karimun, thanks for following up. PLease let us know your final code and quick tip and i’ll add it to the JS documentation as a note thanks

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.

If you enableLinkTracking(), I recommend the class=“piwik_ignore” when you call trackLink() manually, otherwise, trackLink() may be called twice, once by the clickhandler in piwik.js, and again by your custom clickhandler.