fix: streamline datalayer integration for all events

This commit is contained in:
Björn Fromme
2026-03-16 12:02:59 +01:00
parent 9eefdd1858
commit 8529bb0e21
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
});
} }
} }
} }
+5 -2
View File
@@ -1,5 +1,8 @@
<div {{ stimulus_controller('data-layer', { <div {{ stimulus_controller('data-layer', {
'url': url, 'payload': {
'title': title, 'event': 'VirtualPageview',
'virtualPageURL': url,
'virtualPageTitle': title,
'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast' 'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast'
}
}) }}></div> }) }}></div>
+8 -12
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 };
if (consent.advertising) {
dataLayer.push({
'event': 'Transaction', 'event': 'Transaction',
'userInfo': '{{ is_granted('ROLE_USER') ? 'mitglied' : 'gast' }}', 'userInfo': is_granted('ROLE_USER') ? 'mitglied' : 'gast',
'transactionId': '{{ bookingNumber }}', 'transactionId': bookingNumber,
'transactionTotal': {{ bookingTotal }}, 'transactionTotal': bookingTotal,
'transactionProducts': [{ 'transactionProducts': [{
'name': '{{ travelName|e('js') }}', 'name': travelName,
'price': {{ bookingTotal }}, 'price': bookingTotal,
'quantity': 1 'quantity': 1
}] }]
});
} }
</script> }) }}></div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}