Created
November 20, 2023 11:43
-
-
Save PechenkiUA/91788d06acbee809dffca2fedf42e1bf to your computer and use it in GitHub Desktop.
google events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<modification> | |
<name>Google event</name> | |
<code>coder_google_event</code> | |
<version>0.1</version> | |
<date>13.11.2023</date> | |
<author>Oleksandr</author> | |
<link>https://coder.org.ua</link> | |
<!-- перегляд товару --> | |
<file path="catalog/controller/product/product.php"> | |
<operation> | |
<search index="0"><![CDATA[$data['header'] = $this->load->controller('common/header');]]></search> | |
<add position="after"><![CDATA[ | |
$data['google_dataLayer'] = [ | |
'ecomm_prodid'=>$this->request->get['product_id'], | |
'ecomm_pagetype'=> 'product', | |
'ecomm_totalvalue'=> preg_replace('/[^0-9]/i','',$data['special']?:$data['price']), | |
'ecomm_category'=>$category_info['name'] | |
]; | |
]]></add> | |
</operation> | |
</file> | |
<file path="catalog/view/theme/*/template/product/product.twig"> | |
<operation> | |
<search><![CDATA[{{ footer }}]]></search> | |
<add position="after"><![CDATA[ | |
<script data-event="View_Content"> | |
(()=>{ | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.push ({ | |
'event': 'Dynamic Remarketing', | |
'dynamicParams': { | |
'ecomm_prodid': {{ google_dataLayer.ecomm_prodid }}, | |
'ecomm_pagetype': '{{ google_dataLayer.ecomm_pagetype }}', | |
'ecomm_totalvalue': {{ google_dataLayer.ecomm_totalvalue }}, | |
'ecomm_category': '{{ google_dataLayer.ecomm_category }}' | |
} | |
}); | |
})(); | |
</script> | |
]]></add> | |
</operation> | |
</file> | |
<!-- Checkout--> | |
<file path="catalog/view/theme/*/template/checkout/simplecheckout_cart.twig"> | |
<operation> | |
<search><![CDATA[{% for total in totals %}]]></search> | |
<add position="before"><![CDATA[ | |
<script> | |
(function (){ | |
setTimeout(()=>{ | |
const regex = /[^0-9]/gi; | |
const totalValue = document.querySelector('#total_total .simplecheckout-cart-total-value').textContent.replaceAll(regex,'') | |
localStorage.setItem('CheckoutTotalValue',totalValue); | |
let items = document.querySelectorAll('.simplecheckout-cart [data-product-key]'); | |
if(items){ | |
const productKeys = Array.from(items).map(e => e.dataset['productKey']); | |
if(localStorage.getItem('ecomm_prodid') | |
&& JSON.parse(localStorage.getItem('ecomm_prodid')).length != productKeys.length | |
){ | |
} | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.push ({ | |
'event': 'Dynamic Remarketing', | |
'dynamicParams': { | |
ecomm_totalvalue : totalValue, | |
ecomm_prodid: productKeys, | |
ecomm_pagetype: 'cart' | |
} | |
}); | |
localStorage.setItem('ecomm_prodid',JSON.stringify(productKeys)); | |
} | |
},500) | |
})(); | |
</script> | |
]]></add> | |
</operation> | |
</file> | |
<!-- Thank You Page--> | |
<file path="catalog/view/theme/*/template/checkout/success.twig"> | |
<operation> | |
<search><![CDATA[{{ footer }}]]></search> | |
<add position="before"><![CDATA[ | |
{% if google_layer %} | |
<script data-event="Purchase-google"> | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.push ({ | |
'event': 'Dynamic Remarketing', | |
'dynamicParams': { | |
'ecomm_prodid': {{ google_layer.content_ids }}, | |
'ecomm_pagetype': 'purchase', | |
'ecomm_totalvalue ': {{ google_layer.value }}, | |
'ecomm_category ':'{{ google_layer.content_category }}' | |
} | |
}); | |
</script> | |
{% endif %} | |
]]></add> | |
</operation> | |
</file> | |
<file path="catalog/controller/checkout/success.php"> | |
<operation> | |
<search><![CDATA[public function index() {]]></search> | |
<add position="before"><![CDATA[ | |
public function google_data_layer(){ | |
if (! isset($this->session->data['order_id']) && ( empty($this->session->data['order_id'])) ) { | |
return false; | |
} | |
$this->load->model('checkout/order'); | |
$this->load->model('catalog/product'); | |
$products = $this->model_checkout_order->getOrderProducts($this->session->data['order_id']); | |
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); | |
$purchaseData = array(); | |
foreach ($products as $product) { | |
// Отримати інформацію про категорії для товару | |
$categories = $this->getCategoryNameByProductIdGoogle($product['product_id']); | |
// Додати дані про товар до масиву | |
$purchaseData[] = array( | |
'id' => $product['product_id'], | |
'name' => $product['name'], | |
'price' => $product['price'], | |
'quantity' => $product['quantity'], | |
'category' => $categories | |
); | |
} | |
$data = array( | |
'content_ids' => json_encode(array_column($purchaseData, 'id')), | |
'content_name' => implode(', ', array_column($purchaseData, 'name')), | |
'content_category' => implode(', ', array_column($purchaseData, 'category')), | |
'value' => $order_info['total'], // Загальна вартість покупки | |
'currency' => $order_info['currency_code'] // Валюта покупки | |
); | |
return $data ; | |
} | |
public function getCategoryNameByProductIdGoogle($product_id) { | |
$query = $this->db->query("SELECT DISTINCT c.category_id, cd.name FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category c ON (p2c.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE p2c.product_id = '" . (int)$product_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); | |
if ($query->num_rows) { | |
return $query->row['name']; | |
} else { | |
return false; | |
} | |
} | |
]]></add> | |
</operation> | |
<operation> | |
<search><![CDATA[ | |
if (isset($this->session->data['order_id'])) { | |
]]></search> | |
<add position="before"><![CDATA[ | |
$data['google_layer'] = $this->google_data_layer(); | |
]]></add> | |
</operation> | |
</file> | |
</modification> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment