dataLayer and custom events
You can send custom events directly to Google Analytics 4 without making any changes in Google Tag Manager. These events can include up to 5 parameters.
Here is how to send an event named perso_submitted:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({kixelltag_custom_params: null});
window.dataLayer.push({
event: 'kixelltag_custom_event',
kixelltag_custom_params: {
event_name: 'perso_submitted',
}
});
The event attribute must always have the value kixelltag_custom_event. The event name must be specified in kixelltag_custom_params.event_name.
Conditional event triggering
Before sending the event, it is sometimes necessary to ensure that the Google tags are in the right state, i.e. that Consent Mode has been applied. To respect this timing, you should wait until consent is ready. To do so, use the kixelltagOnReady API and listen to the kixelltag_consent_ready event.
Common use cases
- Submitting a form with a redirect to a thank-you page (for example, the contact page).
- Submitting a quote with a redirect to a confirmation page.
To implement this, you can use the kixelltagOnReady API:
window.kixelltagOnReady = window.kixelltagOnReady || [];
window.kixelltagOnReady.push(function (KixellTag) {
KixellTag.on('kixelltag_consent_ready', function() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({kixelltag_custom_params: null});
window.dataLayer.push({
event: 'kixelltag_custom_event',
kixelltag_custom_params: {
event_name: 'perso_submitted',
}
});
});
});
Event parameters
You can add up to 5 parameters to an event. Each parameter must be defined with a name and a value. Here is an example:
window.dataLayer.push({
event: 'kixelltag_custom_event',
kixelltag_custom_params: {
event_name: 'generate_lead',
params: {
key1: 'name1', value1: 'value1',
key2: 'name2', value2: 'value2',
key3: 'name3', value3: 'value3',
key4: 'name4', value4: 'value4',
key5: 'name5', value5: 'value5',
},
}
});
Full example:
When a contact form is submitted successfully, the following code can be used:
window.kixelltagOnReady = window.kixelltagOnReady || [];
window.kixelltagOnReady.push(function (KixellTag) {
KixellTag.on('kixelltag_consent_ready', function() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({kixelltag_custom_params: null});
window.dataLayer.push({
event: 'kixelltag_custom_event',
kixelltag_custom_params: {
event_name: 'generate_lead',
params: {
key1: 'lead_source', value1: 'Contact form'
},
}
});
});
});
This code will send the generate_lead event to Google Analytics 4 with the lead_source parameter containing the value Contact form, with no additional configuration in Google Tag Manager.