adwire lab

Tracking User Roles on a WordPress Site

Learn how to add custom user definitions in Google Analytics 4 (GA4) to better analyze the behavior of different user roles in WordPress. Understand the importance of tracking WordPress user roles to enhance your reports and gain deeper insights into user actions.

Written by Antoine Granjon On 12/08/2024

Understanding and analyzing user behavior is a crucial aspect of digital marketing. However, user actions are often analyzed as a whole, even though traffic consists of different types of users (customers, non-customers, administrators, etc.). To add more granularity to reports, web analytics solutions, including Google Analytics 4, offer the option to create custom “user definitions.” These properties allow you to qualify each user, making your reports more actionable.

In this article, we will focus on adding a "user definition" that can be useful in many cases: the role of the user on WordPress.

The idea is to associate each site user with their WordPress role. This way, in our reports, we can isolate “customers” and obtain reports specific to the activity of users with a customer account, or conversely, isolate non-customer users.

Why Track WordPress User Roles?

On WordPress, different roles can be assigned to site visitors. These roles indicate whether the user is a guest (not logged in), an existing logged-in user, or even an administrator.

Depending on the role assigned to the user, the events generated during a visit will have a different impact on the site's performance. For example, if you have an event corresponding to adding a product to the cart, it might be interesting to know whether the action was performed by an existing customer, a visitor, or an administrator performing tests.

Beyond this example, tracking a WordPress user's role can bring numerous benefits:

  • Proportion of Existing Customers: You can visualize the proportion of logged-in users versus non-logged-in users.
  • Attributing Events to User Clusters: Being able to assign a user role to each event generated on the site allows for better categorization of these events and a better evaluation of their impact on site performance.
  • Creating Segments and Audiences: By assigning a role to each user, you can create segments and isolate a portion of your traffic to better analyze its behavior. Moreover, it will be easy to place a typology of users in a specific audience and exclude users who become customers later.
  • Excluding Administrators: Administrators can generate numerous macro events on a site, so excluding them from analytics allows for cleaner and more representative data.

The Different WordPress Roles

We have mentioned guests (non-logged-in users), customers (logged-in users in the context of an e-shop), and administrators. However, depending on your WordPress configuration, the extensions you use, and the industry you operate in, other roles may be present.

Here is an overview of the different WordPress roles:

  • Guest: A non-logged-in user.
  • Subscriber: A registered user on the site. In the case of using WooCommerce Subscription, Subscribers are users subscribed to a recurring service.
  • Customer: A registered user on the site via a purchase (only if WooCommerce is installed).
  • Contributor: A user with access to publishing features for their own articles.
  • Author: A user with full publishing features access.
  • Editor: A user with full publishing features access, including comment moderation and media management.
  • Administrator: The highest role in WordPress, typically the site owner(s).
  • Super Administrator: An administrator in the case of a multisite WordPress installation.

1. Create User Dimensions in GA4

The first step is to create “user definitions” in GA4. To do this, simply go to Admin, then to “Custom definitions.”

customdef.png

Next, click on the blue “Create custom dimension” button. Here, you will need to specify several things:

customdef2.png

  1. Dimension Name: This is the name of the custom dimension as it will appear in reports.
  2. Scope: Select “User” because we are creating a custom definition related to the user and not an event.
  3. User Property: This is the property as it is transmitted in the DataLayer. It will not be available because it has not been transmitted yet, so you must enter it manually.

2. Display the User Role in the < body > Class

On WordPress, there is a “logged-in” class that is added to the < body > classes when the user visiting the site is logged in. Thus, we can easily send this information to GA4 via the DataLayer.

Before doing this, we will also add a specific class related to the user's role to have it in the < body > classes as well.

To do this, go to the functions.php file of the WordPress theme you are using, then add these lines:

// Function to add the user's role class to the body
function add_role_to_body_class($classes) {
    // Check if the user is logged in
    if (is_user_logged_in()) {
        global $current_user;
        $user_data = get_userdata($current_user->ID);

        if ($user_data) {
            $user_roles = $user_data->roles;

            // Loop through the user's roles
            foreach ($user_roles as $role) {
                // Add the corresponding role class
                $classes[] = 'role-' . $role;
            }
        }
    }
    return $classes;
}
add_filter('body_class', 'add_role_to_body_class');

Now, when a “customer” is logged in on the site, a “role-customer” class will be present in the < body > tag.

3. Send the User Role to the DataLayer

Now that the user’s role appears in the < body > tag, we will create a JavaScript function to retrieve this information and transmit it as a “user definition.”

In this code (using the jQuery framework), we first check if the user is logged in. If not, we assign the “guest” role to the user.

If the user is logged in, we retrieve the “role-*” class present in the body and add the role to the custom definitions that are transmitted.

if($('body').hasClass('logged-in')) {
    var user_logged = true;
    var user_role_class = $('body').attr("class").match(/role-[\w-]*\b/);
    var user_role = user_role_class[0].replace('role-', '');
} else {
    var user_logged = false;
    var user_role = 'guest';
}
dataLayer.push({
    "user_logged" : user_logged,
    "user_role" : user_role
});

4. Assign the User Definition to Each page_view Event

For these two user definitions to be assigned to each page view, we need to integrate them into the Google Tag Manager (GTM) tag that manages GA4.

First, go to GTM, then to “Variables” to create these two variables:

customdef3.png customdef4.png

Next, go to the “Tags” tab and click on the GA4 tag. In the “Configuration Settings” section, add the two user definitions, like this:

customdef5.png

5. Test the Data and Use Custom Definitions in Reports

Now, the “user_logged” and “user_role” user definitions will be populated for each user. As always in GA4, you will need to wait because the data will only be visible the next day.

After this delay, several options are available for using these user definitions.

Create a Filter on GA4 Reports

In any GA4 report, you can add filters. These will allow you to keep only one type of user or, conversely, exclude them.

To do this, go to a GA4 report, then click on “Add Filter.”

In the drop-down menu “Dimension,” select “user_role.” Then choose the user_role(s) you want to keep in your filter.

customdef6.png

Create Audiences

Creating audiences for each of your WordPress roles can be a good way to group existing customers and non-customers. This way, your audiences can be used for remarketing while excluding customer users.

Conversely, you can also create a remarketing campaign targeting only your site’s customers.

To create a custom audience, go to Admin, then Audiences. Click on “New Audience,” then on “Create a New Audience.”

Capture.JPG

Visualize in Looker Studio

To visualize the distribution of users based on their roles, you can use Google Looker Studio. The tool also allows you to create filters to isolate certain roles in your reports.

To start, go to your Looker Studio report. To be able to use the recently added user definitions, you will need to update the data source.

To do this, go to Resource > Manage Added Data Sources.

Select the GA4 source by clicking on “Edit,” and at the bottom, click on “Refresh Fields.”

customdef7.png

The custom definitions “user_logged” and “user_role” are now available in your reports.

Now, you can return to your Looker Studio report and create a “Pie Chart.” The “user_role” dimension will be available in the “Dimension” section.

customdef8.png

These are just examples, but feel free to use these user definitions in filters for your charts, create segments in GA4 to filter your reports more easily, and many other possibilities.

Leave Your Tracking to the Experts!

Maximize the efficiency of your tracking by entrusting it to our team of experts. Contact us for a customized solution and boost your performance.