setCustomData not working


(sunebrodersen) #1

Hi,

Im doing a plugin that tracks some additional data to the log_visit table. But im having some trouble saving data. My trackingcode looks like this:

As seen i have an array with “tracked_userID” that is set using setCustomData. This seems to work but now the question is how to grap that data on the PHP side. I have the following:

public function getListHooksRegistered()
{
	return array(
		'Tracker.newVisitorInformation' => 'logUserID',
	);
}


public function logUserID($notification)
{
$visitorInfo =& $notification->getNotificationObject();
$custom = Piwik_Common::getRequestVar(‘data’);
$visitorInfo[‘tracked_userID’] = $custom[‘tracked_UserID’];
}

But it seems like $custom[‘tracked_UserID’] is empty. If i try to serialize the $custom i get the following:

s:34:"{“tracked_userID”:11751}";

So how am i able to get the “tracked_userID” from the data?

BR/Sune


(sunebrodersen) #2

Hi again,

It seems that i have to replace " with real quoetes like " and then do a json_decode.

But why is this nessecery - its gonna be a “Dirty” solution.

/Sune


(sunebrodersen) #3

So know my code looks like:

public function logUserID($notification)
{
	// retrieve the array of the visitors data from the notification object
	$visitorInfo =& $notification->getNotificationObject();        
	$custom = Piwik_Common::getRequestVar('data');
	
	//This is a diry hack - needs to be fixed!!
	$u = str_replace(""",'"',$custom);
	$s = json_decode($u,true);
	//Hack end!

	       $visitorInfo['tracked_userID'] = $s['tracked_userID'];
}

(vipsoft) #4

Sorry, adding documentation, examples, and helper functions for handling custom data is on our ToDo list…

Piwik_Common::getRequestVar sanitizes the input. So instead of str_replace(), use Piwik_Common::unsanitizeInputValue() before calling json_decode().


(Anita) #5

hi

$custom = Piwik_Common::getRequestVar(‘data’);

what does ‘data’ here refers to?

when i am using it i get the following message…

The parameter ‘data’ isn’t set in the Request, and a default value wasn’t provided


(Kiran) #6

[quote=Anita @ May 7 2010, 10:41 AM]hi

$custom = Piwik_Common::getRequestVar(‘data’);

what does ‘data’ here refers to?

when i am using it i get the following message…

The parameter ‘data’ isn’t set in the Request, and a default value wasn’t provided[/quote]

Hi Anita,

‘data’ is just the name of the variable that is used in the server side(php) for the custom data that you set in the Javascript side.

For. eg, in my test.html page I am setting a custom user tracking ID



var customUserTracking = { 'uid' : '11751kiran'};

piwikTracker.setCustomData(customUserTracking);

piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();


In the above example, I am setting the cstom tracking data. Now in order to use the value in my php code I am going to do the following :



$myCustomData = Piwik_Common::getRequestVar('data');
$cleanData = json_decode(Piwik_Common::unsanitizeInputValue($myCustomData),true);
$this->customUserId = $cleanData['uid'];  

Looks like you are not setting the data in the JS and trying to use it, that is why you are getting the error.

Hope this helps style_emoticons/<#EMO_DIR#>/smile.gif

Bests,
Kiran


(Anita) #7

hi kiran

But I have set the custom data in my tracking page.


(Kiran) #8

[quote=Anita @ May 7 2010, 11:29 AM]hi kiran

But I have set the custom data in my tracking page.[/quote]

Can you share the tracking code where you are setting the value and also the php code so I can see what the problem is style_emoticons/<#EMO_DIR#>/smile.gif


(Anita) #9

hi

Tracking code: sample/index.php

and in my plugin i have added a hook in getListHooksRegistered()

‘Tracker.newVisitorInformation’ => ‘logProviderInfo’,

and in logProviderInfo()
{
$custom = Piwik_Common::getRequestVar(‘data’);
$s = json_decode(Piwik_Common::unsanitizeInputValue($custom),true);
$visitorInfo[‘user_login_id’] = $s[‘userId’];
}

Is using the method logProviderInfo() in my plugin is a problem?


(Kiran) #10

Looks like you are not using the $notification object in your method

Try this :

public function logProviderInfo($notification)
{
	$visitorInfo =& $notification->getNotificationObject();
	$custom = Piwik_Common::getRequestVar('data');
	$s = json_decode(Piwik_Common::unsanitizeInputValue($custom),true);
	$visitorInfo['user_login_id'] = $s['userId'];
}


(Anita) #11

hi kiran

thank you style_emoticons/<#EMO_DIR#>/smile.gif it got updated in my db…


(led-y) #12

Hi,

Whenever some is logged at my website, i add a userid by setCustomData.
Can you tell me, please, how to get the live plugin to output at least a list of these userids?
I couldn’t find out how get this added into the live plugin and i even couldn’t find the customdata at the database :-/ or isn’t it stored by default?

Thanks for help


(Matthieu Aubry) #13

FYI, I will be working on putting ‘Visitor custom variable’ in Piwik in the next release. Keep an eye on the ticket: Custom Variables support: new JS API and new reports · Issue #1984 · matomo-org/matomo · GitHub or the next release.