How do I track a visitor (User ID) across domains (PHP/JS)?

I’d like to link the current visitor’s IP to a specified user ID (via PHP) which Piwik should track across several (sub)domains.

There are several (sub)domains and I want to give a visitor (IP), who is logging in at secure.example.com, a unique UserID to track him on all other domains. The link “IP -> UserID” at login should be made in PHP (Piwik Tracking PHP Client -> setUserId). The “normal” page tracking on all domains (including secure.example.com after login) should be based on JavaScript (Piwik JS tracking snippet).

Example domains:

My current PHP login tracking code (called on secure.example.com/login):


<?php
require_once 'PiwikTracker.php';

$siteId = 3;
$apiUrl = 'http://piwik.example.com/';
$userId = '[TESTUSER]';

$piwik = new PiwikTracker($siteId, $apiUrl);
$piwik->enableCookies('*.example.com');
$piwik->setIp($_SERVER['REMOTE_ADDR']);
$piwik->setUserId($userId);
$piwik->doTrackEvent('Login', 'Login', $userId);
?>

The JS tracking code for all other pages and domains (siteId changes):


<script type="text/javascript">
var _paq = _paq || [];
(function(){ var u="//piwik.example.com/";
_paq.push(['setSiteId', 1]);
_paq.push(['setCookieDomain', '*.example.com']);
_paq.push(['setDomains', '*.example.com']);
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
s.parentNode.insertBefore(g,s); }
)();
</script>

In Piwik config.ini.php I set the following values:


[Tracker]
enable_fingerprinting_across_websites=1
use_third_party_id_cookie = 1
visit_standard_length = 1800
window_look_back_for_visitor = 86400

My problem is:
When I log into secure.example.com and then visit support.example.com (or www.example.com etc.), Piwik does not link these visits to the UserID I set before.
In addition if I overwrite the UserID for the current visitor, it won’t change in Piwik backend.

When I log into secure.example.com and then visit support.example.com (or www.example.com etc.), Piwik does not link these visits to the UserID I set before.

Are both JS codes on both domains setting the same user id AND setting the same Site ID? then Piwik should track them in the same visit… if you are not seeing this, it could be a bug in Piwik.

secure.example.com sets the User ID via PHP once at login (Site ID 3). The JS snippet is used only for tracking before and after login (also Site ID 3). On support.example.com only JS tracks the page visits (Site ID 2) and does not set any User ID (same on www.example.com but with Site ID 1).

I just want to link a User ID with the current IP at login on secure.example.com and then track this user on all other (sub)domains without setting the User ID again (for 24 hours or so).

I too am curious about how one would do this. Our goal for using piwik is less to get raw analytics data, and more geared towards user behavior tracking. I want to be able to tag a user when they log in through a domain that basically acts as a gateway, setting the userId, and make sure the same user is tracked through other domains that are using the same siteId, even if the userId isn’t set on the other domains (because they aren’t dynamic, and aren’t hooked into our login system).

Edit: I see there is this open ticket on github that might effectively solve this problem, though it looks like a ways off. Is there some other way that we could hand off userId data to the link to the next site?

Hi there,

We are testing Piwik for the same purpose as the topic of this thread. We need to get a complete log of the user’s visit across our websites.

We have reviewed your plugin and we see that it relies on the config_id field. In our case this would not work since two users that have the same browser config (browser version, plugins etc…) would have the same config_id and count only as one. Is there another way to track the user across domains and get the complet log we need?

Thank you in advance for your support

Jon

Hi again,

For the record, we just found that this can be done by enabling the use of third party cookies.
More info here How do I enable third party tracking cookies, as well as the existing first party cookies? - Analytics Platform - Matomo

See also: http://plugins.piwik.org/intersites

Hi Matthieu,

Thank you for your response.
We checked that plugin and had it installed but it bases on the config_id the decision whether two visits come from the same user or not. In our tests several visitors had the same config_id.
With the use of the third party cookies we can have unique users correctly tracked across websites.

Hi Joneduca,

The topic is a bit outdated so I hope you’ll receive this response. That is the exact same problem I’m facing at the moment. I want tot track across domains, but don’t really know how to implement it.

Could you maybe explain to me how you did it? So which codes have you edited and added to the pages and with the 3rd party cookie?

I’m looking for this a long time, hope you can help me.

Thanks!

Robin

Hi Robin,

just saw your email. Sorry for the delay. In case you haven’t figured it out or for anybody else who is interesetd, we had to add the following lines to the config.ini file. By doing this, one user will have the same id_visitor across all sites.

[Tracker]
enable_fingerprinting_across_websites = 1
use_third_party_id_cookie = 1
trust_visitors_cookies = 1 

I hope it helps

Cheers

Jon

Hi all,

how are you doing intersite-tracking in piwik v3? The plugin isn’t available for v3.

Hi @leSasch,

We didn’t update yet. Good to know that the plugin is not available yet.

Cheers

hello. did you finish the script. I will use it for https://liftart.com.tr on Turkey. it looks good. regards. serdar

To track a visitor across domains, please our new Cross Domain feature: How do I accurately measure the same visitor across multiple domain names (cross domain linking)? - Analytics Platform - Piwik

Hi Gauheocam,

I could not find, you can check again?