title: Google Analytics: How to generate a unique visitor's User ID? AND add it to the Tracker code
body: <p>According to google...<strong>I need to generate my own user ID's</strong>
  <a href="https://i.stack.imgur.com/lhTLL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lhTLL.png" alt="screenshot"></a></p>
  
  <p>And then add <code>ga('set', 'userId', {{USER_ID}});</code> to my <strong>Tracker code...</strong></p>
  
  <pre><code>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  
  ga('create', 'UA-XXXXXXXX-X', 'auto');
  ga('send', 'pageview');
  </code></pre>
  
  <hr>
  
  <p><br></p>
  
  <h2>Question 1) How to generate my own user ID?</h2>
  
  <p>Right now my code looks like (<code>include ('google-analytics.php')</code>) this:</p>
  
  <pre><code>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  
  ga('create', '&lt;?=$seo['analytics'];?&gt;', 'auto');
  ga('send', 'pageview');
  </code></pre>
  
  <p>How do I get php (or javascript) to generate a unique id, <strong>by site visitor (NOT by clicks)</strong>? </p>
  
  <p>In other words, if the same visitor clicks on several pages within the site, I want the ID to <strong>remain the same ID</strong> and not chage with every click (or refresh).</p>
  
  <p>I tried <code>$seo['userID'] = uniqid();</code> but it changes with every click or refresh.</p>
  
  <hr>
  
  <p><br></p>
  
  <h2>Question 2) Where to insert it, within the tracker code?</h2>
  
  <p>I have no idea where to place <code>ga('set', 'userId', {{&lt;?=$seo['userID'];?&gt;}});</code> within all this...</p>
  
  <pre><code>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  
  ga('create', '&lt;?=$seo['analytics'];?&gt;', 'auto');
  ga('send', 'pageview');
  </code></pre>
  
  <p>...tracker code.</p>
  
  <p>AND if this <code>ga('set', 'userId', {{&lt;?=$seo['userID'];?&gt;}});</code> is the right way to code it, or this <code>ga('set', 'userId', &lt;?=$seo['userID'];?&gt;});</code>, or this <code>ga('set', 'userId', &lt;?=$seo['userID'];?&gt;);</code>, etc...</p>