Skip to content

Commit

Permalink
Добавлена поддержка ecommerce для Я.Метрики и Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Marta Pogosyan committed Dec 1, 2023
1 parent 928fc70 commit eb9f3e9
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 6 deletions.
7 changes: 6 additions & 1 deletion _item.tmpl
@@ -1,4 +1,9 @@
<a href="product.php?product=<TMPL_VAR NAME='code'>" class="item card">
<a href="product.php?product=<TMPL_VAR NAME='code'>" class="item card"
data-id="<TMPL_VAR NAME='code'>"
data-name="<TMPL_VAR NAME='trademark' escape='html'> <TMPL_VAR NAME='model' escape='html'>"
data-brand="<TMPL_VAR NAME='trademark' escape='html'>"
data-category="<TMPL_VAR NAME='category' escape='html'>"
data-price="<TMPL_IF NAME='price'><TMPL_VAR NAME='price'><TMPL_ELSE>0</TMPL_IF>">
<div class="img">
<img src="products/img/<TMPL_VAR NAME='image_big'>" alt="<TMPL_VAR NAME="trademark"> <TMPL_VAR NAME='model'>" title="<TMPL_VAR NAME="trademark"> <TMPL_VAR NAME='model'>"/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions _meta.tmpl
Expand Up @@ -18,4 +18,6 @@

<script src="/theme/js/lib/jquery/jquery.min.js"></script>
<script src="/theme/js/catalog.js"></script>
<script src="/theme/js/ecommerce.js"></script>

<!-- end of _meta.tmpl -->
19 changes: 18 additions & 1 deletion cart.tmpl
Expand Up @@ -52,6 +52,7 @@
<TMPL_LOOP NAME="products">
{
code: "<TMPL_VAR NAME='code'>",
old_qty: <TMPL_VAR NAME='quantity'>,
quantity: <TMPL_VAR NAME='quantity'>,
category: "<TMPL_VAR NAME='category'>",
trademark: "<TMPL_VAR NAME='trademark'>",
Expand All @@ -67,6 +68,22 @@
DELIVERY_PRICE = <TMPL_VAR NAME='DELIVERY_PRICE'>;
DELIVERY_FREE_LIMIT = <TMPL_VAR NAME='DELIVERY_FREE_LIMIT'>;
DELIVERY_EXTRA_PAY = <TMPL_VAR NAME='DELIVERY_EXTRA_PAY'>;

<TMPL_IF NAME="just_added_products">
ecommerce_add([
<TMPL_LOOP NAME="just_added_products">
{
id: "<TMPL_VAR NAME='code'>",
name: "<TMPL_VAR NAME='trademark' escape='html'> <TMPL_VAR NAME='model' escape='html'>",
price: <TMPL_VAR NAME='price'>,
brand: "<TMPL_VAR NAME='trademark' escape='html'>",
category: "<TMPL_VAR NAME='category' escape='html'>",
quantity: <TMPL_VAR NAME='quantity'>,
},
</TMPL_LOOP>
]);
</TMPL_IF>

</script>

<script type="x-template" id="cartapp-template">
Expand Down Expand Up @@ -243,8 +260,8 @@
</div>
</div>

</div>
</template>
</div>
</script>

<TMPL_INCLUDE FILE="_footer.tmpl">
Expand Down
5 changes: 5 additions & 0 deletions cat.tmpl
Expand Up @@ -23,5 +23,10 @@
</div>

<TMPL_INCLUDE FILE="_footer.tmpl">

<script type="text/javascript">
ecommerce_impressions();
</script>

</body>
</html>
14 changes: 10 additions & 4 deletions index.tmpl
Expand Up @@ -14,16 +14,16 @@
<div class="container">

<div class="tiles">
<a href="cat.php?cat=amper">
<a href="cat.php?cat=amper" class="promo" data-id="TILES-INDEX" data-name="Плитки на главной" data-creative="Надёжные стабилизаторы: Вольт Engineering" data-position="Плитка-1">
<img src="/theme/img/tiles/tile-volt.jpg">
</a>
<a href="cat.php?cat=stab3">
<a href="cat.php?cat=stab3" class="promo" data-id="TILES-INDEX" data-name="Плитки на главной" data-creative="Трехфазные стабилизаторы: Вольт Engineering" data-position="Плитка-2">
<img src="/theme/img/tiles/tile-3-phase.jpg">
</a>
<a href="cat.php?cat=converter">
<a href="cat.php?cat=converter" class="promo" data-id="TILES-INDEX" data-name="Плитки на главной" data-creative="ИБП для газовых котлов" data-position="Плитка-3">
<img src="/theme/img/tiles/tile-ups-boiler.jpg">
</a>
<a href="cat.php?cat=welding">
<a href="cat.php?cat=welding" class="promo" data-id="TILES-INDEX" data-name="Плитки на главной" data-creative="Всё для сварки" data-position="Плитка-4">
<img src="/theme/img/tiles/tile-welding.jpg">
</a>
</div>
Expand All @@ -42,5 +42,11 @@
</div>

<TMPL_INCLUDE FILE="_footer.tmpl">

<script type="text/javascript">
ecommerce_impressions();
ecommerce_promoview();
</script>

</body>
</html>
34 changes: 34 additions & 0 deletions js/cart.js
Expand Up @@ -122,13 +122,36 @@ function onDeleteProductClick(product){
this.ajaxDeleteProduct(product);
this.updateCounterIcon();
this.updateCargo();
ecommerce_remove([{
'id': product.code,
'name': product.trademark + ' ' + product.model,
'brand': product.trademark,
'category': product.category,
'price': product.price,
'quantity': product.quantity,
}]);
}

/////////////////////////////////////////////
function onProductQuantityChange(product){
this.ajaxUpdateCart();
this.updateCounterIcon();
this.updateCargo();
if (product.old_qty != product.quantity) {
prod = {
'id': product.code,
'name': product.trademark + ' ' + product.model,
'brand': product.trademark,
'category': product.category,
'price': product.price,
'quantity': Math.abs(product.quantity - product.old_qty),
}
if (product.quantity > product.old_qty)
ecommerce_add([prod]);
else
ecommerce_remove([prod]);
product.old_qty = product.quantity;
}
}

/////////////////////////////////////////////
Expand Down Expand Up @@ -418,6 +441,17 @@ function onSubmit() {
var res = data.match(/Order: (\d+)/i);
if (res.length == 2) {
instance.order_num = parseInt(res[1]);
var order_products = instance.products.map(function(item){
return {
'id': item.code,
'name': item.trademark + ' ' + item.model,
'brand': item.trademark,
'category': item.category,
'price': item.price,
'quantity': item.quantity,
};
});
ecommerce_purchase(instance.order_num, order_products);
} else {
errorMsg('Неизвестная ошибка. Попробуйте еще раз.');
}
Expand Down
164 changes: 164 additions & 0 deletions js/ecommerce.js
@@ -0,0 +1,164 @@
/////////////////////////////////////////////////////////////////////////
function ecommerce_purchase(id, products) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"purchase": {
"actionField": { "id" : id, },
"products": products,
}
}
});
}

/////////////////////////////////////////////////////////////////////////
function ecommerce_add(products) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"add": {
"products": products,
}
}
});
}

/////////////////////////////////////////////////////////////////////////
function ecommerce_remove(products) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"remove": {
"products": products,
}
}
});
}

/////////////////////////////////////////////////////////////////////////
function ecommerce_detail(product) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"detail": {
"products": [{
"id": product.id,
"name" : product.name,
"price": product.price,
"brand": product.brand,
"category": product.category,
}]
}
}
});
}

///////////////////////////////////////////////////////////////////
function ecommerce_impressions(arg1='h1', list_selector=".items-list") {
if ($.isArray(arg1)) {
var impressions = []
arg1.forEach(function(item, index, arr) {
if (item.hasOwnProperty('title_selector') && item.hasOwnProperty('list_selector')) {
var impr = ecommerce_getSingleListImpressions(item.title_selector, item.list_selector);
impressions = impressions.concat(impr);
}
});
} else {
var impressions = ecommerce_getSingleListImpressions(arg1, list_selector);
}
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"impressions": impressions,
},
});
}

////////////////////////////////////////////////////////////////////////
function ecommerce_getSingleListImpressions(title_selector, list_selector=".items-list") {
var title = $(title_selector).text();
var impressions = [];
$(list_selector + ' > *').each(function(index){
var position = index + 1;
$(this).data('list', title);
$(this).data('position', position);
$(this).on('click', ecommerce_onListItemClick);
impressions.push({
'id': $(this).data('id'),
'name': $(this).data('name'),
'brand': $(this).data('brand'),
'category': $(this).data('category'),
'price': $(this).data('price'),
'list': title,
'position': position,
});
});
return impressions;
}

////////////////////////////////////////////////////////////////////////
function ecommerce_onListItemClick(e) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"currencyCode": "RUB",
"click": {
"products": [{
"id": $(this).data('id'),
"name": $(this).data('name'),
"price": $(this).data('price'),
"brand": $(this).data('brand'),
"category": $(this).data('category'),
"list": $(this).data('list'),
"position": $(this).data('position'),
}],
}
}
});
return true;
}

/////////////////////////////////////////////////////////////////////////
function ecommerce_promoview(promo_selector = ".promo") {
var promotions = []
$(promo_selector).each(function(index){
$(this).on('click', ecommerce_onPromoClick);
promotions.push({
"id": $(this).data('id'),
"name": $(this).data('name'),
"creative": $(this).data('creative'),
"position": $(this).data('position'),
})
});
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"promoView": {
"promotions": promotions,
}
}
});
}

////////////////////////////////////////////////////////////////////////
function ecommerce_onPromoClick(e) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
"ecommerce": {
"promoClick": {
"promotions": [{
"id": $(this).data('id'),
"name": $(this).data('name'),
"creative": $(this).data('creative'),
"position": $(this).data('position'),
}],
}
}
});
return true;
}
8 changes: 8 additions & 0 deletions product.tmpl
Expand Up @@ -167,6 +167,14 @@
DELIVERY_PRICE = <TMPL_VAR NAME="DELIVERY_PRICE">;
DELIVERY_FREE_LIMIT = <TMPL_VAR NAME="DELIVERY_FREE_LIMIT">;
DELIVERY_EXTRA_PAY = <TMPL_VAR NAME="DELIVERY_EXTRA_PAY">;

ecommerce_detail({
"id": "<TMPL_VAR NAME='code'>",
"name" : "<TMPL_VAR NAME='trademark'> <TMPL_VAR NAME='model'>",
"price": <TMPL_IF NAME="price"><TMPL_VAR NAME="price"><TMPL_ELSE>0</TMPL_IF>,
"brand": "<TMPL_VAR NAME='trademark'>",
"category": "<TMPL_VAR NAME='category'>",
});
</script>

</body>
Expand Down

0 comments on commit eb9f3e9

Please sign in to comment.