Changing custom variables between pages?

I want to setup a custom variable to highlight people who sign up multiple times at the site. So, when the user signs up for the first time, I record a custom variable for that:

$customer_action_tracker->setCustomVariable(1, ‘Signup’, ‘New’, ‘visit’);

Then, if he tries to sign up again during the same session, I want to read that variable and if it is set already - change it to:

$customer_action_tracker->setCustomVariable(1, ‘Signup’, ‘Multiple’, ‘visit’);

That doesn’t work though - when the user loads a new page I create a new tracking object and it is blank:

print_r($customer_action_tracker->getCustomVariable(1, ‘visit’)); - prints an empty space, and not an array with Signup=>New as I expected.
What am I missing?

The cookies are not set in the PHP tracker. This is a known limitation/missing feature. It would be nice if the code was setting the cookies (like the JS tracker) does. Normally they are stored in 1st party cookies in JS but are therefore unavailable here. Maybe you can submit pull request to fix this if you’re inspired :wink:

I am not sure if that is needed. The visit is being recognised as one visit - when I go to the report, I see the same visit with repeated pageviews of the signup page, and the signup itself reported with the custom variable I set. The only problem is I cannot write and then read the custom variable from one pageview to another.

You could also read the custom var for this particular Visitor ID using the Live API: Real Time Analytics - Analytics Platform - Matomo (a different API separate from the Tracking API)

I don’t understand how to do it… The documentation suggests I can get a report through this API for segments based on the custom variables. But I need the value of this variable for a particular person.

Is there an example somewhere of a Piwik implementation of some sort of action counter? E.g., “the same user clicked on a link so many times”, or “the same user view this page so many times” etc. (not “how many times the link was clicked” but “how many times did the same person do it” )?

You can call the API, for example with segment with &visitorId==XXXX segment, which you can get with getVisitorId()

So… this is what I am doing:

$url = ‘http://’.$this->application->config->ANALYTICSURL.’/’.
’?module=API&method=CustomVariables.getCustomVariables&expanded=1’.
’&idSite=1’.
’&period=week&date=today’.
’&segment=visitorId==’.$customer_action_tracker->getVisitorId().
’&filter_sort_column=idvisit&filter_sort_order=desc&filter_limit=5’.
’&token_auth=’.$this->application->config->ANALYTICSTOKEN.
’&format=PHP’;

I get ALL the data for this visitor for the last week, including several different values I set for the custom variables.
&filter_sort_column=idvisit&filter_sort_order=desc - this doesn’t seem to change anything, and filter_limit only limits the number of CustomVariables I get, not the number of rows I want to explore. In other words - how do I limit the call to only get the LAST row that was recorded for this visitor?

ps Another thing - it doesn’t seem to be real-time. If I record an action and then try to get it through the API, it comes up blank (no data). If I then go and delete the archive tables and have them re-generated, I get the variables. Is there any way to get data from the real-time DB tables?

See the “timeout” setting in: How to Set up Auto-Archiving of Your Reports - Analytics Platform - Matomo