You can invoke the JavaScript SDK’s consent API once the user consent is available. The SDK then comes out of the pre-consent mode and resumes normal functioning.

A sample implementation for a custom provider is shown below:

<script type = "text/javascript">
  // consent provider callback
  function ConsentManagerWrapper() { /// Pseudo name
    if (window.isConsented()) { // Pseudo name

      // Pass the allowed and denied category IDs for custom setup
      rudderanalytics.consent({
        options: {
          trackConsent: true / false, // Optional; default is false
          consentManagement: {
            allowedConsentIds: ['<category_id_1>','<category_id_2>',.....], // Required for Custom provider
            deniedConsentIds: ['<category_id_3>','<category_id_4>',.....]
          }, // Required for Custom provider
          storage: {
            type: "cookieStorage", // Other supported values are "localStorage","sessionStorage", "memoryStorage", and "none"
            entries: {
              userId: {
                type: "localStorage" // Other supported values are "cookieStorage","sessionStorage", "memoryStorage", and "none"
              },
              userTraits: {
                type: "cookieStorage" // Other supported values are "localStorage","sessionStorage", "memoryStorage", and "none"
              },
              sessionInfo: {
                type: "cookieStorage" // Other supported values are "localStorage","sessionStorage", "memoryStorage", and "none"
              }
            }
          }, // Optional
          integrations: IntegrationOpts, // Optional
          discardPreConsentEvents: true / false, // Optional; default is false
          sendPageEvent: true / false // Optional, default is false
        }
      });
    }
  } 
</script>

The consent API options are listed below:

ParameterTypeDescription
trackConsentBooleanDetermines if the SDK should send a track event with the name Consent Management Interaction.

Default value: false
consentManagementObjectLets you pass the user consent data in case of a custom consent management provider. The SDK requires the allowedConsentIds and deniedConsentIds fields in case of a Custom consent provider.
storageObjectLets you configure the different storage-specific options like:

  • storage.type: Specify where the persisted data should be stored.
  • storage.entries: Lets you define storage for specific type of persisted user data.
integrationsObjectInstructs the SDK to filter the integrations before the consent filtering takes effect.
discardPreConsentEventsBooleanDetermines if the SDK should discard all the pre-consent events buffered previously.

Default value: false
sendPageEventBooleanDetermines if the SDK should send a page event.

Default value: false

The SDK does the following once you invoke the consent API:

  • Loads the device mode integrations based on consent.
  • Fetches the consent information from the consent manager.
  • Stores persistent user information like userId, anonymousId, traits, etc. according to the specified storage option.
  • Discards or replays the buffered pre-consent events to the destinations based on the discardPreconsentEvents parameter.

The SDK also sends any events received after the user gives consent to the destinations immediately.


Questions? Contact us by email or on Slack