title: Piwik: How do I track a visitor (User ID) across domains (PHP/JS)?
body: <p>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.</p>
  
  <p>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).</p>
  
  <p>Example domains:</p>
  
  <ul>
  <li>www.example.com (siteId 1)</li>
  <li>support.example.com (siteId 2)</li>
  <li>secure.example.com (siteId 3)</li>
  <li>www.anotherexample.com (siteId 4)</li>
  </ul>
  
  <p>My current PHP login tracking code (called on secure.example.com/login):</p>
  
  <pre><code>&lt;?php
  require_once 'PiwikTracker.php';
  
  $siteId = 3;
  $apiUrl = 'http://piwik.example.com/';
  $userId = '[TESTUSER]';
  
  $piwik = new PiwikTracker($siteId, $apiUrl);
  $piwik-&gt;enableCookies('*.example.com');
  $piwik-&gt;setIp($_SERVER['REMOTE_ADDR']);
  $piwik-&gt;setUserId($userId);
  $piwik-&gt;doTrackEvent('Login', 'Login', $userId);
  ?&gt;
  </code></pre>
  
  <p>The JS tracking code for all other pages and domains (siteId changes):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  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); }
  )();
  &lt;/script&gt;
  </code></pre>
  
  <p>In Piwik config.ini.php I set the following values:</p>
  
  <pre><code>[Tracker]
  enable_fingerprinting_across_websites=1
  use_third_party_id_cookie = 1
  visit_standard_length = 1800
  window_look_back_for_visitor = 86400
  </code></pre>
  
  <p>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.</p>