Simple Analytics

Simple Analytics is a privacy-first analytics platform that avoids storing personally identifiable information (PII), making it ideal for organizations that need simple, compliant analytics without cookies.

This document explains how to integrate Didomi with Simple Analytics to send user consent status as events.

To illustrate the possibilities with Simple Analytics, we will present two use cases (you may include one, the other, or both):

  1. Opt-in and Opt-out: The user either accepts at least one vendor or denies all vendors (with none enabled).

  2. Selective Opt-in or Opt-out: The user grants or denies consent to a specific vendor (e.g., Google).

How the integration works

To integrate Didomi with Simple Analytics, you must ensure that:

  • The Simple Analytics script tag is set before the Didomi SDK.

  • The Didomi SDK is ready when sending the first "before" consent status to Simple Analytics.

  • Consent updates are sent to Simple Analytics using the Didomi consent.changed event.

To achieve the first two items, we use a condition to verify whether user consent should be collected. When that condition is met, a “before” event is sent to the Simple Analytics dashboard. This helps you monitor users who see the consent notice before making a choice.

Then, to assess each consent decision and send it to Simple Analytics, we use the consent.changed event. This event allows us to send a dedicated consent_update event to Simple Analytics whenever the user's choices change.

You can also use the consent values metadata in multiple ways in Simple Analytics, such as in Goals or the Events explorer. To explore the possibilities further, please refer directly to the associated Simple Analytics metadata documentation.

Scenarios

Prerequisites (required for both use cases)

Use case 1: Opt-ins and Opt-outs

When to Use

Use this when you need to collect Simple Analytics metadata on:

  • Opt-ins: At least one vendor enabled.

  • Opt-outs: At least one vendor disabled and no vendor enabled.

  • Mixed: At least one vendor enabled and at least one vendor disabled.

Implementation code

<script>
  window.didomiEventListeners = window.didomiEventListeners || [];
  window.didomiEventListeners.push({
    event: "consent.changed",
    listener: () => {
      const userStatus = Didomi.getCurrentUserStatus();
      const vendors = userStatus.vendors || {};

      const enabledVendors = Object.values(vendors).filter((v) => v.enabled === true);
      const disabledVendors = Object.values(vendors).filter((v) => v.enabled === false);

      const hasEnabledVendors = enabledVendors.length > 0;
      const hasDisabledVendors = disabledVendors.length > 0;

      // We will use below the same calculation methods defined in our SDK and detailed here: https://support.didomi.io/cmp-notice-metrics-indicators

      // ✅ Opt-in: At least one vendor enabled
      const isOptIn = hasEnabledVendors;

      // ❌ Opt-out: At least one vendor disabled AND none enabled
      const isOptOut = hasDisabledVendors && !hasEnabledVendors;

      // ⚠️ Mixed: At least one vendor enabled AND at least one vendor disabled
      const isMixed = hasEnabledVendors && hasDisabledVendors;

      if (isMixed) {
        consentStatus = "mixed";
      } else if (isOptIn) {
        consentStatus = "approved";
      } else if (isOptOut) {
        consentStatus = "denied";
      }

      if (!window.sa_loaded) {
        // If the Simple Analytics script is not loaded, no action is taken.
        console.warn("Simple Analytics script is not loaded.");
        return;
      }

      if (window.sa_event) {
        sa_event("consent_update", { consent_status: consentStatus }); // We change the event name to `consent_update`
      }
    },
  });

  window.didomiOnReady = window.didomiOnReady || [];
  window.didomiOnReady.push(function (Didomi) {
    let consentStatus = "before";

    /**
     * This function allows detecting the number of times consent should be collected, to compare with the actual consent given
     * It will only return "before" if consent collection is still required for this user.
     *
     * ⚠️ Note: If this causes "before" to be sent too frequently (e.g., due to new vendors being added),
     * you can alternatively use: `Didomi?.notice?.isVisible?.()` to gate it based on the banner actually being shown.
     */
    if (!window.sa_loaded) {
      // If the Simple Analytics script is not loaded, no action is taken.
      console.warn("Simple Analytics script is not loaded.");
      return;
    }

    if (window.sa_event && Didomi?.shouldUserStatusBeCollected?.()) {
      sa_event("consent_init", { consent_status: consentStatus }); // We first define the event name to `consent_init`
    }
  });
</script>

// You must declare this tag before the Didomi SDK, otherwise you won't be able to collect "before" events
<script type="text/javascript" data-collect-dnt="true" async src="https://scripts.simpleanalyticscdn.com/latest.js"></script>

We have used consent_init and consent_update as event names in the example above, but you can, of course, choose any other names that make more sense to you.

Use case 2: Selective Opt-in/Opt-out (only Google is enabled or disabled)

When to Use

Use this if you need to track consent for a specific vendor, such as Google, whether it is enabled or disabled.

Implementation code

<script>
  window.didomiEventListeners = window.didomiEventListeners || [];
  window.didomiEventListeners.push({
    event: "consent.changed",
    listener: () => {
      const userStatus = Didomi.getCurrentUserStatus();
      const vendorId = "google";

      // ✅ Check if Google is enabled
      const hasConsent = userStatus.vendors && userStatus.vendors.hasOwnProperty(vendorId) && userStatus.vendors[vendorId].enabled;

      consentStatus = hasConsent ? "approved" : "denied";

      if (!window.sa_loaded) {
        // If the Simple Analytics script is not loaded, no action is taken.
        console.warn("Simple Analytics script is not loaded.");
        return;
      }

      if (window.sa_event) {
        sa_event("consent_update", { consent_status: consentStatus }); // We change the event name to `consent_update`
      }
    },
  });

  window.didomiOnReady = window.didomiOnReady || [];
  window.didomiOnReady.push(function (Didomi) {
    let consentStatus = "before";

    /**
     * This function allows detecting the number of times consent should be collected, to compare with the actual consent given
     * It will only return "before" if consent collection is still required for this user.
     *
     * ⚠️ Note: If this causes "before" to be sent too frequently (e.g., due to new vendors being added),
     * you can alternatively use: `Didomi?.notice?.isVisible?.()` to gate it based on the banner actually being shown.
     */
    if (!window.sa_loaded) {
      // If the Simple Analytics script is not loaded, no action is taken.
      console.warn("Simple Analytics script is not loaded.");
      return;
    }

    if (window.sa_event && Didomi?.shouldUserStatusBeCollected?.()) {
      sa_event("consent_init", { consent_status: consentStatus }); // We first define the event name to `consent_init`
    }
  });
</script>

// You must declare this tag before the Didomi SDK, otherwise you won't be able to collect "before" events
<script type="text/javascript" data-collect-dnt="true" async src="https://scripts.simpleanalyticscdn.com/latest.js"></script>

Additional notes

You can extend this guide with additional vendor-specific or purpose-based events using the same structure.

Example use cases:

  • Consent tracking per purpose (e.g., marketing)

  • Custom event segmentation based on combined vendor states

Resources

Last updated