E-Commerce order tracked with wrong user

I’ve semi-successfully implemented the e-commerce tracking. The only problem I still have is that the actual order that’s being tracked is not being assigned to the right user. It always ends up creating a new visitor ID, so the actions that lead to the actual purchase are never linked to it.

I do all the tracking in JS except for the addEcommerceItem() and doTrackEcommerceOrder() calls. They are run on the PHP side, using the class provided by you handsome people.

I’ve already tried manually setting

 $tracker->setIp( $_SERVER["REMOTE_ADDR"] ); 

before any of the calls mentioned above, but that didn’t help either. So how can I fix that?

$tracker->setIp should work.
However make sure you also call $tracker->setTokenAuth

as the token is needed to overwrite the IP address.

I do that as well as per documentation, of course. Just didn’t think it was worth mentioning.

Could it have anything to do with the IP anonymization? It’s set to strip the last part from IPs.

Ok, my answer was incorrect.

The best way to ensure that your orders are tracked for the right visitor is that your PHP script knows the “Visitor ID” of the user purchasing on your website.

  • You can get the visitor id from Piwik javascript code:

_paq.push([ function() { visitor_id = this.getVisitorId(); }]);

  • Then your goal is to record this somewhere so that you can access it in your PHP script
  • then call the setVisitorId to force the ID on the ecommerce purchase request:

$tracker->setVisitorId($visitorId);

Then your ecommerce order should be recorded in the existing visit for this visitor.

Thanks for the answer. I shall try that and see how it goes.

Ok, I’m a bit lost. The site I’m working on is still using the old (deprecated) code, so:


_paq.push([ function() { visitor_id = this.getVisitorId(); }]);

looks alien to me. How would I run that the old way?

And while we’re at it - what does that actually do? Store the visitor ID in the visitor_id variable in JS? Or does it create a custom variable which I can fetch on the server side? If not, would that be a good way to solve my problem?

If not, then I’d have to use XHR probably, right? Is there a callback or would I write the code right inside that function you’re passing into the array?

Thanks for the help.

in general / typically you may store the visitor ID in a column in the ecommerce order being prepared, so that you can send it to Piwik with the order itself. If you have trouble about this, maybe I could create a FAQ to explain as I remember explaining 2-3 times before.

This will also get easier once we implement User ID as you could then pass the email address as user id which I guess you collect before the order. Cheers

Nah, I’m good, thanks. I ended up POSTing it to the server via XHR on each page view (it’s a small site) and storing it in the PHP session (if different).

Another thing: I tried to track the payment option the visitor used from the server as custom variable after the actual purchase, but it seems I have to do that before calling trackPageView(), which I don’t on the server. Would it hurt if I did that anyway, even if there is no actual page view? Doing it on the client side is problematic since I may not have the data at the right moment, or they may change their mind, leading to wrong, duplicate trackings.

can you explain what you try to achieve and what you do in steps? but likely yes you’d have to call setCustomVariable before trackEcommerceOrder

Again, thanks for the help. I guess you’re busy, so this is much appreciated.

Re first issue though: would it be a security risk to store the visitorId as an additional cookie? I know it’s already buried in the cookie Piwik sets itself, but it doesn’t feel right to regex it out of there. Doesn’t seem risky to me though.

Other issue: On the server I’m calling addEcommerceItem(), then doTrackEcommerceOrder() and then setCustomVariable(). That didn’t track the variable though. So what you’re saying is, if I exchange the last two calls, it should work? I’ll try.

Changing the order didn’t work.

I know it’s already buried in the cookie Piwik sets itself, but it doesn’t feel right to regex it out of there.

you can get visitor id like this:


var visitor_id;
_paq.push([ function() { visitor_id = this.getVisitorId(); }]);

addEcommerceItem(), then doTrackEcommerceOrder() and then setCustomVariable().
call in this order:


setCustomVariable()
addEcommerceItem()
doTrackEcommerceOrder()