fix: streamline datalayer integration for all events

This commit is contained in:
Björn Fromme
2026-01-28 19:40:48 +01:00
parent 93eb08c610
commit 677ffc7368
3 changed files with 20 additions and 28 deletions
+2 -9
View File
@@ -2,19 +2,12 @@ import { Controller } from '@hotwired/stimulus';
export default class extends Controller { export default class extends Controller {
static values = { static values = {
url: String, payload: Object
title: String,
userInfo: String
}; };
connect() { connect() {
if (typeof window.dataLayer !== 'undefined') { if (typeof window.dataLayer !== 'undefined') {
window.dataLayer.push({ window.dataLayer.push(this.payloadValue);
'event': 'VirtualPageview',
'virtualPageURL': this.urlValue,
'virtualPageTitle': this.titleValue,
'userInfo': this.userInfoValue
});
} }
} }
} }
+6 -3
View File
@@ -1,5 +1,8 @@
<div {{ stimulus_controller('data-layer', { <div {{ stimulus_controller('data-layer', {
'url': url, 'payload': {
'title': title, 'event': 'VirtualPageview',
'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast' 'virtualPageURL': url,
'virtualPageTitle': title,
'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast'
}
}) }}></div> }) }}></div>
+12 -16
View File
@@ -59,22 +59,18 @@
{% block data_layer %} {% block data_layer %}
{% if bookingTotal is not null and travelName is not null %} {% if bookingTotal is not null and travelName is not null %}
<script> <div {{ stimulus_controller('data-layer', {
window.dataLayer = window.dataLayer || []; 'payload': {
var consent = (window.CookieFirst && window.CookieFirst.consent) || { advertising: false }; 'event': 'Transaction',
if (consent.advertising) { 'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast',
dataLayer.push({ 'transactionId': bookingNumber,
'event': 'Transaction', 'transactionTotal': bookingTotal,
'userInfo': '{{ is_granted('ROLE_USER') ? 'mitglied' : 'gast' }}', 'transactionProducts': [{
'transactionId': '{{ bookingNumber }}', 'name': travelName,
'transactionTotal': {{ bookingTotal }}, 'price': bookingTotal,
'transactionProducts': [{ 'quantity': 1
'name': '{{ travelName|e('js') }}', }]
'price': {{ bookingTotal }},
'quantity': 1
}]
});
} }
</script> }) }}></div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}