Search code examples
phpgoogle-analyticsanalytics

Google Analytics User Tracking


is it possible to differentiate with Google Analytics between logged in and logged out users? Can I even track the data of each unique user by sending the User ID somehow via JS? If yes, how?


Solution

  • You can redirect logged in user on a specific url if a specific google analytics id than a logged out one:

    <?php
        if($user->isLoggedIn()) {
                echo '
        your google analytics code here for logged in users   
            '
            ;
            } else {
               echo '
        your google analytics code here for public users 
            '
            ;
        }
    

    FOR EXAMPLE, in the google analytics documentation:

    if (isset($userId)) {
      $gacode = "ga('create', 'UA-XXXX-Y', { 'userId': '%s' });";
      echo sprintf($gacode, $userId);
    } else {
      $gacode = "ga('create', 'UA-XXXX-Y');";
      echo sprintf($gacode);
    }
    

    You can find more information here:

    https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id