# Custom events

If you have your own custom events that you use as triggers for tags and also want to add an additional condition on the consent status of the user, you need to make sure that your custom events only fire after the Didomi event that you are using (so that the Didomi variables have already been populated). You have two options to do so.

### Option 1: Wrap your custom events

In your JavaScript code that is pushing your custom events onto the data layer, wrap all your custom events to only fire them after the Didomi SDK has initialized and the `didomi-ready` event has been sent:

```javascript
window.didomiOnReady = window.didomiOnReady || [];
window.didomiOnReady.push(function (Didomi) {
  // Fire your custom event(s)
  dataLayer.push({'event': 'custom_event'});
});
```

You can use as many `didomiOnReady` functions and put them anywhere in your code. Custom events that are not used as triggers for a tag that you want to condition on consent do not need to be wrapped.

{% hint style="danger" %}
If you do not wait until the Didomi SDK is ready before firing your custom events, the order of the events will end up being random and you have no guarantee that the consent is correctly enforced for the tags using custom events triggers.
{% endhint %}

### Option 2: Condition your custom events

The second option is to only fire your custom events when you have consent. In that case, you do not need to add a consent condition in GTM and the mere fact that the event gets fired implies that consent was given

&#x20;It is not ideal because is pushes the consent condition into your JavaScript code but can be useful sometimes.

```javascript
function fireCustomEvents(consentGiven) {
    if (consentGiven === true) {
      // Fire your custom event(s) because the user has given consent
      dataLayer.push({'event': 'custom_event'});
    }
  }
  
  window.didomiOnReady = window.didomiOnReady || [];
  window.didomiOnReady.push(function (Didomi) {
    // The SDK is done loading, check the user status for a given vendor
    const consentGiven = Didomi.getCurrentUserStatus().vendors['vendor-id']?.enabled;
  
    if (consentGiven === true) {
      // The user has enabled that vendor, fire the custom events
      fireCustomEvents(consentGiven);
    } else {
      // Subscribe to the consent.changed event to get notified when the consent status changes
      Didomi.on('consent.changed', function () {
        // The consent status of the user has changed, check again
        fireCustomEvents(Didomi.getCurrentUserStatus().vendors['vendor-id']?.enabled);
      });
    }
  });
```
