Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Last active January 9, 2024 13:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PechenkiUA/289de13d6e52dd29fc96de70c05159ae to your computer and use it in GitHub Desktop.
Save PechenkiUA/289de13d6e52dd29fc96de70c05159ae to your computer and use it in GitHub Desktop.
GA Event
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>GA Event</name>
<code>default</code>
<version>1.0</version>
<author>Coder</author>
<link></link>
<!-- перегляд товару view_item -->
<file path="catalog/view/theme/*/template/product/product.tpl">
<operation>
<search><![CDATA[<?php echo $footer; ?>]]></search>
<add position="before"><![CDATA[
<script data-event="View_Content">
(()=>{
window.dataLayer = window.dataLayer || [];
window.dataLayer.push ({
'event': 'view_item',
'items': [{
'item_name': '<?php echo $gdataLayer['item_name'];?>',
'item_id': <?php echo $gdataLayer['item_id'];?>,
'price': '<?php echo $gdataLayer['price'];?>',
'item_brand': '<?php echo $gdataLayer['item_brand'];?>',
'item_category': '<?php echo $gdataLayer['item_category'];?>'
}]
});
})();
</script>
]]></add>
</operation>
</file>
<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['gdataLayer'] = $this->gProductReturn($this->request->get['product_id']);
]]></add>
</operation>
<operation>
<search><![CDATA[
private $error = array();
]]></search>
<add position="after"><![CDATA[
public function getCatName($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 array_map(function ($s){
return $s['name'];
},$query->rows);
} else {
return false;
}
}
public function gProduct(){
if (!isset($this->request->get['p_id']))
return;
echo json_encode($this->gProductReturn($this->request->get['p_id']));
return;
}
public function gProductReturn($product_id){
$mod_p = 'catalog/product';
$mod_m = 'catalog/manufacturer';
$this->load->model($mod_p);
$this->load->model($mod_m);
$this->load->model('catalog/category');
$product_info = $this->model_catalog_product->getProduct($product_id);
$category_name = $this->getCatName($product_id);
$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($product_info['manufacturer_id']);
$item_brand = '';
if(isset($manufacturer_info['name'])){
$item_brand = $manufacturer_info['name'];
}
header('Content-Type: application/json');
$quantity = 1;
$gprice = 0;
if($product_info['special']){
$gprice = number_format($product_info['special'],2,'.','');
}else{
$gprice = number_format($product_info['price'],2,'.','');
}
return [
'item_id' => (int) $product_id,
'price' => $gprice,
'item_category' =>( isset($category_name[0])? $category_name[0] : ''),
'item_category2' => ( isset($category_name[1]) ? $category_name[1] : ''),
'item_name' => $product_info['name'],
'item_brand' => $item_brand,
'quantity'=> $quantity
];
}
]]></add>
</operation>
</file>
<!-- перегляд атегорії -->
<file path="catalog/controller/product/category.php">
<operation>
<search trim="true"><![CDATA[
if ((float)$result['special']) {
]]></search>
<add position="before"><![CDATA[
$data['gaitem'][] = $this->load->controller('product/product/gProductReturn',$result['product_id']);
]]></add>
</operation>
</file>
<file path="catalog/view/theme/*/template/product/category.tpl">
<operation>
<search><![CDATA[<?php echo $footer; ?>]]></search>
<add position="before"><![CDATA[
<script>
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object.
dataLayer.push({
event: "view_item_list",
ecommerce: {
items: <?php echo json_encode($gaitem);?>
}
});
</script>
]]></add>
</operation>
</file>
<file path="catalog/view/theme/*/template/common/footer.*">
<operation>
<search>
<![CDATA[</body>]]>
</search>
<add position="before">
<![CDATA[
<script id='add_to_cart'>
$(document).ajaxSuccess(function(event, xhr, settings) {
if(settings.url.includes('checkout/cart/add')){
const searchParams = new URLSearchParams(settings.data);
const pID = searchParams.get('product_id');
const quantity = searchParams.get('quantity');
if (!pID) return;
$.ajax({ type: 'get',
url: 'index.php/?route=product/product/gProduct&p_id='+pID,
dataType: 'json',
cache: false,
success: function(json) {
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: "add_to_cart",
ecommerce: {
items: [{
item_name: json.item_name,
item_id: pID,
price: json.price,
item_brand: json.item_brand,
item_category: json.item_category,
quantity: quantity
}]
}
});
}
});
}
});
/* ocdev_smart_cart&remove */
function setGa(p_id,action,quantity = 1){
if (!p_id) return;
$.ajax({ type: 'get',
url: 'index.php/?route=product/product/gProduct&p_id='+p_id,
dataType: 'json',
cache: false,
success: function(json) {
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: action,
ecommerce: {
items: [{
item_name: json.item_name,
item_id: p_id,
price: json.price,
item_brand: json.item_brand,
item_category: json.item_category,
quantity: quantity
}]
}
});
}
});
}
</script>
]]>
</add>
</operation>
</file>
<file path="catalog/controller/checkout/success.php">
<operation>
<search><![CDATA[public function index() {]]></search>
<add position="before"><![CDATA[
public function getOrderInfoById($order_id) {
$order_info = array();
// Отримання загальної ціни без доставки
$query = $this->db->query("SELECT total FROM " . DB_PREFIX . "order WHERE order_id = '" . (int)$order_id . "'");
$order_info['total'] = $query->row['total'];
// Отримання ціни доставки
$query = $this->db->query("SELECT value FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "' AND code = 'shipping'");
$order_info['shipping_price'] = $query->row['value'];
// Отримання купона
$query = $this->db->query("SELECT code FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "' AND code = 'coupon'");
$order_info['coupon_code'] = ($query->num_rows > 0) ? $query->row['code'] : '';
return $order_info;
}
public function GaThanksFunction(){
if (! isset($this->session->data['order_id']) && ( empty($this->session->data['order_id'])) ) {
return false;
}
$this->load->model('checkout/order');
$this->load->model('account/order');
$this->load->model('catalog/product');
$products = $this->model_account_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) {
$info = $this->load->controller('product/product/gProductReturn',$product['product_id']);
$info['quantity'] = (int) $product['quantity'];
$items[] = $info;
}
$totalOrder = $this->getOrderInfoById($this->session->data['order_id']);
$data = array(
'currencyCode' => $order_info['currency_code'],
'purchase'=>[
'actionField'=>[
'id' => $this->session->data['order_id'],
'revenue'=> $totalOrder['total'],
'shipping'=>$totalOrder['shipping_price'],
'coupon'=> $totalOrder['coupon_code'],
]
],
'products'=>$items
);
return $data ;
}
]]></add>
</operation>
<operation>
<search><![CDATA[
if (isset($this->session->data['order_id'])) {
]]></search>
<add position="before"><![CDATA[
$data['gaThanks'] = $this->GaThanksFunction();
]]></add>
</operation>
</file>
<file path="catalog/view/theme/*/template/common/success.tpl">
<operation>
<search><![CDATA[<?php echo $footer; ?>]]></search>
<add position="before"><![CDATA[
<?php if($gaThanks): ?>
<script data-event="Purchase">
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: "purchase",
ecommerce: <?php echo json_encode($gaThanks); ?>
});
</script>
<?php endif; ?>
]]></add>
</operation>
</file>
<!-- видати потім-->
<!--<file path="catalog/controller/payment/pp_express.php">
<operation>
<search trim="true"><![CDATA[
public function index() {
]]></search>
<add position="before"><![CDATA[
public function __construct($registry) {
parent::__construct($registry);
if ($this->session->data['user_id']) {
$this->config->set('pp_express_test',1);
echo $this->config->get('pp_express_test');
}
}
]]></add>
</operation>
</file>-->
</modification>
@dj-pasha
Copy link

dj-pasha commented Jan 9, 2024

Заміни ecommerce: <?php echo json_encode(gaThanks); ?> на ecommerce: <?php echo json_encode($gaThanks); ?>
Та
Заміни <file path="catalog/view/theme/*/template/checkout/success.tpl"> на <file path="catalog/view/theme/*/template/common/success.tpl">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment