Tracking logged in vs. non-logged in users in Google Analytics

Use custom dimensions in Google Analytics.

  1. First, navigate to the "admin" section of Google Analytics' web portal, click "Custom Definitions", then "Custom Dimensions" under the property you're selecting. Click "new custom dimension", give the dimension a name, and save it as type "hit".

Screenshot borrowed from the blog post

  1. Now add code similar to the following:

    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('set', {
      dimension1: __isUserLoggedIn__,
    ga('send', 'pageview');

    The bit marked as __isUserLoggedIn__ should be replaced with the relevant code. The number in dimension1 should match the number of the custom dimension created in the previous step.

For more details about this, see this great blog post on CSS Tricks.

I finally found it in the Google Analytics Docs:

Use session-level custom variables to distinguish different visitor experiences across sessions.

For example, if your website offers users the ability to login, you can use a custom variable scoped to the session level for user login status. In that way, you can segment visits by those from logged in members versus anonymous visitors.

   1,             // This custom var is set to slot #1.  Required parameter.
   'User Type',   // The name of the custom variable.  Required parameter.
   'Member',      // Sets the value of "User Type" to "Member" or "Visitor" depending on status.  Required parameter.
   2              // Sets the scope to session-level.  Optional parameter.