Search code examples
phpgoogle-analyticsgoogle-analytics-apiuniversal-analytics

Track user through Google Universal Analytics


I want to track a user from my site, give an id to Google Analytics and later be able to see which pages that id visited and for how long did that id view the pages.

I've tried custom variables with the classic analytics

I tried enabling the User-ID feature in the Universal Analytics

None of these have given me what I want yet. Is there any way to accomplish this with the Universal Analytics?

UPDATE

This is a sample code with a user whose id is 2. I try to set the userid in different ways to see if at least in one way it shows.

    <script>
  (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-XXXXXXX-1', 'domain.com', { 'userId': '2' });

  ga('set', '&uid', '2'); // Set the user ID using signed-in user_id.

  var dimensionValue = '2';
  ga('set', 'dimension1', dimensionValue);

  ga('send', 'pageview');

</script>

Solution

  • Userid is a feature that stitches together sessions from authenticated users over multiple devices. It is not displayed in the interface and will not accomplish what you are after.

    However you can set a custom dimension. You will either have to create segments (a PITA since you'd need a segment for each unique user) or you will have to create custom reports, as custom dimensions will not show in standard reports [1]. So codewise you're fine, if you need advice on custom report creation I'd guess that [email protected] is more suited for that (since this does not involve code).

    However I do not think GA is really a suitable tool to track indidual users, it's designed to analyze aggregate data from many users and visits.

    [1] at least not as primary dimension, you can set them as secondary dimension.