{% block disco_meta_extra_information_extensions_products %}
{% set productsExtension = context.context.extensions.disGA4ProductsExtension %}
{% if productsExtension is not null %}
{% block disco_meta_extra_information_extensions_products_initialisation %}
<script {% if app.session.get('csp_nonce') %}nonce="{{ app.session.get('csp_nonce') }}"{% endif %}
type="text/javascript">
window.ga4Product = {};
</script>
{% endblock %}
{% set bigCategoriesList = ga4GetCategoriesSQLCached(productsExtension,context.context) %}
{% block disco_meta_extra_information_extensions_products_loop %}
{% for product in productsExtension %}
{% set price = product.calculatedPrice %}
{% if product.calculatedPrices|length == 1 %}
{% set price = product.calculatedPrices.first %}
{% endif %}
{% set listPrice = price.listPrice %}
{% if listPrice.percentage > 0 %}
{% set listingPrice = price.listPrice.price %}
{% else %}
{% set listingPrice = price.unitPrice %}
{% endif %}
{% if product.calculatedPrices.count > 0 %}
{% set startPrice = product.calculatedPrices.first.unitPrice %}
{% set hasGraduatedPrice = true %}
{% else %}
{% set startPrice = price.unitPrice %}
{% set hasGraduatedPrice = false %}
{% endif %}
{% set variantText = '' %}
{% if product.variation %}
{% for variation in product.variation %}
{% set variantText = variantText ~ ' ' ~ variation.group ~ ' ' ~ variation.option ~ ' -' %}
{% endfor %}
{% endif %}
{% block disco_meta_extra_information_extensions_product_product_id %}
{% set ga4Id = 'ga4-product-' ~ product.id %}
{% endblock %}
{% if product.manufacturer %}
{% set itemBrand = product.manufacturer.translated.name %}
{% else %}
{% set itemBrand = ga4GetManufacturerSQL(product.manufacturerId,context.context) %}
{% endif %}
{% block disco_meta_extra_information_extensions_product_array_script %}
<script {% if app.session.get('csp_nonce') %}nonce="{{ app.session.get('csp_nonce') }}"{% endif %}
type="text/javascript">
window.ga4Product['{{ product.id }}'] = {
item_brand: '{{ itemBrand }}',
item_name: '{{ product.translated.name }}',
item_id: '{{ product.productNumber }}',
currency: '{{ context.currency.translated.shortName }}',
item_variant: '{{ variantText | trim(' ','left') | trim('-','right') }}',
price: '{{ price.unitPrice }}',
extra: {
minPurchase: '{{ product.minPurchase }}',
shopware_id: '{{ product.id }}',
realPrice: '{{ listingPrice }}',
item_startPrice: '{{ startPrice }}',
item_hasGraduatedPrice: '{{ hasGraduatedPrice }}',
taxRate: '{{ product.tax.taxRate }}',
cheapest: '{{ price.unitPrice }}',
}
};
{% if controllerName|lower == 'product' %}
window.ga4Product['{{ product.id }}']['item_list_id'] = 'product';
window.ga4Product['{{ product.id }}']['item_list_name'] = 'product';
{% elseif controllerName|lower == 'navigation' %}
window.ga4Product['{{ product.id }}']['item_list_id'] = '{{ page.header.navigation.active.id }}';
window.ga4Product['{{ product.id }}']['item_list_name'] = '{{ page.header.navigation.active.name }}';
{% elseif controllerName|lower == 'search' %}
window.ga4Product['{{ product.id }}']['item_list_id'] = 'search';
window.ga4Product['{{ product.id }}']['item_list_name'] = 'search';
{% endif %}
{% if bigCategoriesList[product.id] %}
{% set counter = 1 %}
{% for key,value in bigCategoriesList[product.id] %}
{% if counter == 1 %}
window.ga4Product['{{ product.id }}']['item_category'] = '{{ value }}';
{% else %}
window.ga4Product['{{ product.id }}']['item_category{{ counter }}'] = '{{ value }}';
{% endif %}
{% set counter = counter + 1 %}
{% endfor %}
{% endif %}
</script>
{% endblock %}
{% endfor %}
{% endblock %}
{% endif %}
{% endblock %}