CloudFlare Zaraz for WordPress: Alternative to Tag Manager Server-Side Tracking

Written by

Jannick

Published on

BlogUncategorized

What is Server-Side Tracking?

Server-side tracking is a different approach to the collection of data of users. It takes aways the tracking from the browser of the user to a server. The biggest different is that client-side tracking relies on JavaScript running in the user’s browser, which will be blocked by ad blockers and privacy settings in browsers. Server-side tracking moves the capturing of data to the server, resulting in better data accuracy, better control over data privacy and improved website performance.

By managing data on your server, you can bypass browser restrictions, enhance data security, and maintain compliance with privacy regulations like GDPR, all while gaining deeper insights into user behavior.

What is CloudFlare Zaraz?

Cloudflare Zaraz is a tool designed to enhance website performance, security, and privacy by offloading the loading of third-party tools (like Google Analytics 4) to CloudFlare’s edge network.

Loading third-party tools using the edge network of CloudFlare minimizes the impact on a website’s load time and reduces the need for processing on the side of the browser. Zaraz has integrations with tools like Google Analytics, Facebook Pixel, TikTok, and more.

How to install CloudFlare Zaraz?

To install Cloudflare Zaraz make sure you have your domain setup in Cloudflare. Aren’t using CloudFlare yet? Then you first need to move your domain over to Cloudflare.

Let’s start!

  • In Cloudflare, go to the overview of the domain you want to enable Zaraz on
  • In the left-hand-side panel, go to Zaraz
  • Click on Add new tool under Third-Party Tools
  • Select the tool you want to load server-side using Zaraz, for example Google Analytics 4
  • Follow the prompts to configure the tool.
    • For example, for Google Analytics, you will need to enter your Tracking ID or Measurement ID

And that’s it, you’ve successfully installed and started using Cloudflare Zaraz on your website! In Zaraz there are a lot more advanced options to set up. To learn more about setting up Cloudflare Zaraz go here.

Tips & Tricks

Prevent Google Analytics 4 from triggering for a WordPress Admin

After setting up Google Analytics 4 you’ll notice Zaraz will also make Google Analytics track the sessions WordPress admins. Follow these steps to make sure WordPress admins won’t be tracked (logged in and logged out).

  • First step, we need to add a function to your WordPress installation. There are two ways:
    • Edit the functions.php file
    • Use a plug-in to add PHP code to your website
      • I will use the plug-in Admin and Site Enhancements (ASE) Pro in the steps that follow

The function code to add is the following:

function setAdminCookie() {
  if (current_user_can( 'update_core' )) {
    if(!isset($_COOKIE['is_admin'])) {
      // set a cookie for 1 year 
      setcookie('is_admin', "true", time()+31556926);
    }
  }
}
add_action('init', 'setAdminCookie');
  • To add this code first open the settings of Admin and Site Enhancements (ASE) Pro
  • Go to ‘Custom code’
  • Make sure ‘Code Snippets Manager’ is enabled in the Custom Code settings
  • Now, go to ‘Code Snippets’ in the left-hand menu of WordPress
  • Click on the button ‘Add PHP snippet’
  • Give the snippet a title and add the function code (shown above) in the black code field.
  • Now hit ‘Publish’

You’ve now successfully configured WordPress to create a cookie in the browser of the user that tells Cloudflare Zaraz the session on the website belongs to an admin of the website.

After this you’ll need to edit a few settings in Cloudflare Zaraz by following these steps:

  • Log in to Cloudflare
  • Go to your Domain and go into the settings of Zaraz
  • Go the sub-menu items Triggers and hit the button ‘Create Trigger’
  • Give the new trigger the name ‘Logged In Users’
  • Set up the Match Rule for this trigger in the following way:
    • Rule type: Match rule
    • Variable name: Cookie: is_admin
    • Match operation: Contains
    • Match value: true
cleanshot 2024 05 17 at 15.42.32
  • Double check and if your settings match, hit ‘Save’.

You’ve now successfully created a trigger that will trigger when it notices the cookie ‘is_admin’ with the value ‘true’. This trigger needs to be added to the Google Analytics 4 integration to make sure Google Analytics 4 will be blocked for admins. To do this follow these steps:

  • Go to the Third-party tools sub-menu and click on ‘Google Analytics 4’
  • Click on ‘Settings’
  • In the first setting of the settings windows you’ll see Blocking Triggers. Select the newly made Logged In Users trigger.
  • Hit ‘Save’ and in the top-right corner.
cleanshot 2024 05 17 at 15.59.07
  • Now click on the Action ‘Pageview’ and setup the Blocking trigger Logged In Users for the Pageview action.
cleanshot 2024 05 17 at 15.57.59
  • Hit ‘Save’ and hit the ‘Publish x Changes’ button to have the change take effect.

And your done, sessions from WordPress admins won’t be tracked anymore in Google Analytics 4.