Created
November 10, 2021 07:41
-
-
Save yuriy-yarvinen/1d1de3f90992c73f539ecb67c526d72a to your computer and use it in GitHub Desktop.
ecomerce yandex metrika mail tmr dataLayer
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
// Данные | |
var dataLayer = dataLayer || []; | |
var _tmr = _tmr || []; | |
var catalog = document.querySelector('.category-products'); // Каталог товаров | |
var product = document.querySelector('.product-category-title'); // Страница с товаром | |
// На странице с товаром | |
if (product){ | |
var id = document.getElementById('product_addtocart_form').querySelector('input[name="product"]').value; // Идентификатор | |
var productName = document.querySelector('.bk_name').textContent; // Название товара | |
var price = parseInt(document.querySelector('.bk_price').textContent.replace(/(\,00)|\D+/g,"")); // Цена | |
// Проверка на наличие значений | |
if (id && price && productName){ | |
// Просмотр страницы | |
_tmr.push({ | |
type: 'itemView', | |
productid: id, | |
pagetype: 'product', | |
list: '1', | |
totalvalue: price | |
}); | |
// Просмотр страницы | |
dataLayer.push({ | |
"ecommerce": { | |
"currencyCode": "RUB", | |
"detail": { | |
"products": [ | |
{ | |
"id": id, | |
"name": productName, | |
"price": price | |
} | |
] | |
} | |
} | |
}); | |
// Добавление в корзину | |
document.querySelector('.btn-cart').onclick = function(){ | |
_tmr.push({ | |
type: 'itemView', | |
productid: id, | |
pagetype: 'cart', | |
list: '1', | |
totalvalue: price | |
}); | |
dataLayer.push({ | |
"ecommerce": { | |
"currencyCode": "RUB", | |
"add": { | |
"products": [ | |
{ | |
"id": id, | |
"name": productName, | |
"price": price, | |
"quantity": 1 | |
} | |
] | |
} | |
} | |
}); | |
} | |
} | |
} | |
// На странице каталога | |
else if (catalog) { | |
// Все товары на странице | |
var products = catalog.querySelectorAll('.item'); | |
if (products) { | |
products.forEach(function(prod){ | |
var cart = prod.querySelector('.btn-cart'); | |
if (cart) { | |
cart.onclick = function(){ | |
var id = null; // Идентификатор | |
var productName = prod.querySelector('.product-name a').textContent; // Название товара | |
var price = null; // Цена товара | |
// Товар со скидкой | |
if (prod.classList.contains('sale-product')){ | |
id = prod.querySelector('.special-price .price').id.replace(/\D+/, ""); | |
price = parseInt(prod.querySelector('.special-price .price').textContent.replace(/(\,00)|\D+/g,"")); // Специальная цена | |
} | |
// Обычный товар | |
else { | |
id = prod.querySelector('.regular-price').id.replace(/\D+/, ""); | |
price = parseInt(prod.querySelector('.price').textContent.replace(/(\,00)|\D+/g,"")); // Обычная цена | |
} | |
if (id && price && productName){ | |
_tmr.push({ | |
type: 'itemView', | |
productid: id, | |
pagetype: 'cart', | |
list: '1', | |
totalvalue: price | |
}); | |
dataLayer.push({ | |
"ecommerce": { | |
"currencyCode": "RUB", | |
"add": { | |
"products": [ | |
{ | |
"id": id, | |
"name": productName, | |
"price": price, | |
"quantity": 1 | |
} | |
] | |
} | |
} | |
}); | |
} | |
} | |
} | |
}); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment