',
PRODUCT_TYPE: 'id', // id or code
GTAG_CURRENCY_TYPE: 'currency',
SEND_MEMBER: 'true',
GTAG_PRICE_WITH_TAX: parseInt('1'),
},
config: {
optimize_id: ''
},
setParams: () => {
for (const [key, value] of Object.entries(GTAG_APP.params)) {
GTAG_APP.params[key] = value === '{{' + key + '}}' || value === '' ? '' : value;
}
},
run: () => {
GTAG_APP.setParams();
window.dataLayer = window.dataLayer || [];
gtag('js', new Date());
GTAG_APP.setConfig();
gtag('config', GTAG_APP.params.GA4_TRACKING_ID, GTAG_APP.config);
if (GTAG_APP.params.GTAG_ADWORDS_ID) {
gtag('config', GTAG_APP.params.GTAG_ADWORDS_ID, window.MEMBER_INFO.ID > 0 ? {'allow_enhanced_conversions': true} : {});
}
GTAG_APP.setUserData();
GTAG_APP.productDetail()
GTAG_APP.pageCategory()
if (typeof THEME_VERSION !== 'undefined') {
GTAG_APP.dynamicLoader();
}else{
GTAG_APP.dynamicLoaderV4();
}
GTAG_APP.addToCart()
GTAG_APP.wishList()
GTAG_APP.login()
GTAG_APP.register()
GTAG_APP.orderApprove()
GTAG_APP.pageCart()
GTAG_APP.orderAddress()
GTAG_APP.orderPayment()
GTAG_APP.removeFromCart();
GTAG_APP.beginCheckout();
},
categoryTree: (categoryPath) => {
categoryPath = categoryPath.split(' > ');
if (categoryPath[categoryPath.length - 1] === '') {
delete categoryPath[categoryPath.length - 1];
}
let categoryTree = [];
categoryPath.forEach((category, index) => {
categoryTree['item_category' + (index === 0 ? '' : index + 1)] = category;
});
return categoryTree;
},
setUserData: () => {
if (GTAG_APP.params.SEND_MEMBER === 'true' && window.MEMBER_INFO.MAIL !== '') {
gtag('set', 'user_data', {
'email': window.MEMBER_INFO.MAIL,
'address': {
'first_name': window.MEMBER_INFO.FIRST_NAME,
'last_name': window.MEMBER_INFO.LAST_NAME,
'city': window.MEMBER_INFO.MEMBER_CITY,
'country': window.MEMBER_INFO.MEMBER_COUNTRY,
'postal_code': window.MEMBER_INFO.MEMBER_ZIP_CODE,
}
})
}
},
setConfig: () => {
if (GTAG_APP.params.SEND_MEMBER === "true" && window.MEMBER_INFO.ID > 0) {
GTAG_APP.config.user_id = window.MEMBER_INFO.ID;
}
},
getCartItems: async () => {
await TSOFT_APPS.get.cart.load();
const cartLoad = await TSOFT_APPS.get.result.cart.load;
if (cartLoad === null) {
return {
items: [],
value: 0,
currency: GTAG_APP.currency,
}
}
const products = cartLoad.PRODUCTS.map((product, index) => {
let salePrice = parseFloat(parseFloat(product.PRICE_SELL).toFixed(2));
let discount = 0;
if (product.IS_DISCOUNT_ACTIVE) {
let priceNotDiscounted = parseFloat(parseFloat(product.PRICE_NOT_DISCOUNTED).toFixed(2));
discount = parseFloat(parseFloat(priceNotDiscounted - salePrice).toFixed(2));
}
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (product.VAT / 100))).toFixed(2));
}
let item_variant = '';
if (typeof product.VARIANT1_NAME !== 'undefined'){
item_variant = product.VARIANT1_NAME;
}
if (typeof product.VARIANT2_NAME !== 'undefined'){
item_variant += ' ' + product.VARIANT2_NAME;
}
return {
item_id: GTAG_APP.params.PRODUCT_TYPE === 'id' ? product.ID : product.PRODUCT_CODE,
item_name: product.TITLE,
index: index,
item_brand: product.BRAND,
item_list_id: product.CATEGORY_ID,
item_list_name: product.CATEGORY_NAME,
price: salePrice,
discount: discount > 0 ? discount : 0,
quantity: parseInt(product.COUNT),
item_variant: item_variant,
...GTAG_APP.categoryTree(product.CATEGORY_PATH + product.CATEGORY_NAME),
}
});
return {
items: products,
value: parseFloat(parseFloat(cartLoad.PRICE_TOTAL_SELL).toFixed(2)),
coupon: cartLoad.COUPON_CODE,
currency: GTAG_APP.currency
}
},
productDetail: () => {
if (window.PAGE_TYPE === "product") {
const productData = PRODUCT_DATA[0];
let salePrice = parseFloat(parseFloat(productData.sale_price).toFixed(2));
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (productData.vat / 100))).toFixed(2));
}
let discount = 0;
if (productData.total_base_price > productData.total_sale_price){
discount = parseFloat((productData.total_base_price - productData.total_sale_price).toFixed(2));
}
gtag("event", "view_item", {
"currency": GTAG_APP.currency,
'value': parseFloat(parseFloat(productData.sale_price).toFixed(2)),
"items": [
{
"item_id": productData[GTAG_APP.params.PRODUCT_TYPE],
"item_name": productData.name,
"index": 0,
"item_brand": productData.brand,
"item_list_id": productData.category_id,
"item_list_name": productData.category,
"price": salePrice,
"discount": discount > 0 ? discount : 0,
"quantity": parseInt(productData.quantity) > 0 ? 1 : 0,
...GTAG_APP.categoryTree(productData.category_path + productData.category),
}
]
});
}
},
pageCategory: () => {
if (window.PAGE_TYPE === "category") {
let category = {
name: '',
id: '',
}
if (typeof CATEGORY_DATA !== 'undefined')
{
category = CATEGORY_DATA;
}
const products = PRODUCT_DATA;
gtag('event', 'view_item_list', {
'item_list_name': category.name,
'item_list_id': category.id,
'items': products.slice(0, 200).map((product, index) => {
let salePrice = parseFloat(parseFloat(product.sale_price).toFixed(2));
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (product.vat / 100))).toFixed(2));
}
let discount = 0;
if (product.total_base_price > product.total_sale_price){
discount = parseFloat((product.total_base_price - product.total_sale_price).toFixed(2));
}
return {
item_id: product[GTAG_APP.params.PRODUCT_TYPE],
item_name: product.name,
index: index,
item_brand: product.brand,
item_list_id: product.category_id,
item_list_name: (product.category_path + product.category).replace(/ > /g, '/'),
price: salePrice,
discount: discount > 0 ? discount : 0,
...GTAG_APP.categoryTree(product.category_path + product.category),
}
})
});
}
},
dynamicLoader: () => {
TSOFT_APPS.product.dynamicLoader.push(function (products) {
let sentProductIds = new Set();
const observer = new IntersectionObserver((entries) => {
const newlyVisibleProducts = [];
entries.forEach(entry => {
if (entry.isIntersecting) {
const el = entry.target;
const productTitle = el.querySelector('.product-title') ? el.querySelector('.product-title').textContent : '';
// products içinde product.name i = title olanı bul
const product = products.find(p => p.name === productTitle);
if(typeof product !== 'undefined' && !sentProductIds.has(product.id)){
sentProductIds.add(product.id);
newlyVisibleProducts.push(product);
observer.unobserve(el);
}
}
});
if(newlyVisibleProducts.length > 0){
gtag('event', 'view_item_list', {
'items': newlyVisibleProducts.map((product, index) => {
let salePrice = parseFloat(parseFloat(product.sale_price).toFixed(2));
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (product.vat / 100))).toFixed(2));
}
let discount = 0;
if (product.total_base_price > product.total_sale_price){
discount = parseFloat((product.total_base_price - product.total_sale_price).toFixed(2));
}
return {
id: product[GTAG_APP.params.PRODUCT_TYPE],
name: product.name,
list_name: product.category,
brand: product.brand,
category: (product.category_path + product.category).replace(/ > /g, '/'),
list_position: index + 1,
quantity: parseInt(product.quantity) > 0 ? 1 : 0,
price: salePrice,
discount: discount > 0 ? discount : 0
}
}),
});
}
}, {
threshold: 0.5 // %50'si görünüyorsa tetiklenir
});
document.querySelectorAll('.product-item').forEach(el => observer.observe(el))
});
},
dynamicLoaderV4: () => {
TSOFT_APPS.product.dynamicLoader.push(function (products) {
gtag('event', 'view_item_list', {
'items': products.slice(0, 200).map((product, index) => {
let salePrice = parseFloat(parseFloat(product.sale_price).toFixed(2));
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (product.vat / 100))).toFixed(2));
}
let discount = 0;
if (product.total_base_price > product.total_sale_price){
discount = parseFloat((product.total_base_price - product.total_sale_price).toFixed(2));
}
return {
id: product[GTAG_APP.params.PRODUCT_TYPE],
name: product.name,
list_name: product.category,
brand: product.brand,
category: (product.category_path + product.category).replace(/ > /g, '/'),
list_position: index + 1,
quantity: parseInt(product.quantity) > 0 ? 1 : 0,
price: salePrice,
discount: discount > 0 ? discount : 0
}
}),
});
});
},
addToCart: () => {
TSOFT_APPS.cart.callback.add.push(function (r) {
if (([-1, 0].includes(r.status))) {
return false;
}
const productData = PRODUCT_DATA[0];
let salePrice = parseFloat(parseFloat(productData.sale_price).toFixed(2));
if (GTAG_APP.params.GTAG_PRICE_WITH_TAX) {
salePrice = parseFloat((salePrice + (salePrice * (productData.vat / 100))).toFixed(2));
}
let discount = 0;
if (productData.total_base_price > productData.total_sale_price){
discount = parseFloat((productData.total_base_price - productData.total_sale_price).toFixed(2));
}
let addedProduct = Array.isArray(r) ? r[0].addedProduct : r.addedProduct;
gtag("event", "add_to_cart", {
'currency': GTAG_APP.currency,
'value': parseFloat(parseFloat(productData.sale_price).toFixed(2) * parseInt(addedProduct.quantity)),
'items': [
{
'item_id': addedProduct[GTAG_APP.params.PRODUCT_TYPE],
'item_name': addedProduct.name,
'index': 0,
'item_brand': addedProduct.brand,
'item_list_name': addedProduct.category,
'price': salePrice,
"discount": (discount > 0 ? discount : 0) * parseInt(addedProduct.quantity),
'quantity': parseInt(addedProduct.quantity),
...GTAG_APP.categoryTree(addedProduct.category_path + addedProduct.category),
}
]
});
})
},
wishList: () => {
TSOFT_APPS.product.wishList.callback.add.push(function (r) {
if (r.status) {
let product = r.data[0].product;
gtag("event", "add_to_wishlist", {
'currency': GTAG_APP.currency,
'value': parseFloat(parseFloat(product.price).toFixed(2)),
'items': [
{
item_id: product[GTAG_APP.params.PRODUCT_TYPE],
item_name: product.name,
index: 0,
item_list_name: product.category_name,
quantity: 1,
price: parseFloat(parseFloat(product.price).toFixed(2)),
...GTAG_APP.categoryTree((product.category_path + product.category_name) || '')
}
],
});
}
})
},
login: () => {
TSOFT_APPS.page.tracking.login.callback.push(function (params) {
gtag("event", "login", {});
})
},
register: () => {
TSOFT_APPS.page.tracking.sign.callback.push(function (params) {
if ([1, 2].includes(params.status)) {
gtag("event", "sign_up", {});
}
})
},
pageCart: () => {
TSOFT_APPS.page.cart.callback.push(async function (data) {
const cart = await GTAG_APP.getCartItems();
if (cart.items.length === 0) {
return false;
}
gtag("event", "view_cart", cart);
})
},
orderAddress: () => {
TsoftAppsOrderAddressCallback(async function () {
const cart = await GTAG_APP.getCartItems();
if (cart.items.length === 0) {
return false;
}
gtag("event", "add_shipping_info", cart);
gtag('event', 'checkout_progress', {
'items': cart.items,
'checkout_step': 2
});
});
},
orderPayment: () => {
TsoftAppsOrderPaymentCallback(async function (params) {
const cart = await GTAG_APP.getCartItems();
if (cart.items.length === 0) {
return false;
}
gtag('event', 'checkout_progress', {
'items': cart.items,
'checkout_step': 3
});
gtag("event", "add_payment_info", cart);
})
},
removeFromCart: () => {
TSOFT_APPS.cart.callback.delete.push(function (params) {
let deleteObj = false;
if (params?.deletedProduct?.id){
deleteObj = params.deletedProduct;
}else if (params?.type === 'decrement' && params?.addedProduct?.id){
deleteObj = params.addedProduct;
}
if (params.status && deleteObj?.id) {
gtag("event", "remove_from_cart", {
currency: GTAG_APP.currency,
value: parseFloat(parseFloat(deleteObj.price * deleteObj.quantity).toFixed(2)),
items: [
{
item_id: deleteObj.id,
item_name: deleteObj.name,
index: 0,
item_brand: deleteObj.brand,
price: parseFloat(parseFloat(deleteObj.price).toFixed(2)),
quantity: parseInt(deleteObj.quantity)
}
]
});
}
})
},
orderApprove: () => {
TsoftAppsOrderApproveCallback(function (order) {
const currency = order[GTAG_APP.params.GTAG_CURRENCY_TYPE] === 'TL' ? 'TRY' : order[GTAG_APP.params.GTAG_CURRENCY_TYPE];
let taxPrice = parseFloat(parseFloat(order.total_vat).toFixed(2));
const products = order.products.map((product, index) => {
const price = GTAG_APP.params.GTAG_CURRENCY_TYPE === 'currency' ? product.total_amount_order : product.total_amount_store
console.log(product);
let discount = 0;
if (product.total_base_amount - product.total_amount > 0 ) {
let priceNotDiscounted = parseFloat(parseFloat(product.total_base_amount).toFixed(2));
discount = parseFloat(parseFloat(priceNotDiscounted - product.total_amount).toFixed(2));
}
return {
item_id: GTAG_APP.params.PRODUCT_TYPE === 'id' ? product.identifier : product.code,
item_name: product.name,
index: index,
item_brand: product.brand,
item_list_id: product.category_id,
item_list_name: product.category_name,
quantity: parseInt(product.quantity),
price: parseFloat(parseFloat(price).toFixed(2)),
discount: discount > 0 ? discount : 0,
...GTAG_APP.categoryTree(product.category_path + product.category_name)
}
});
let orderValue = products.reduce((acc, product) => {
return acc + (product.price * product.quantity);
}, 0);
gtag("event", "purchase", {
'transaction_id': order.transaction,
'value': parseFloat(parseFloat(orderValue - taxPrice).toFixed(2)),
'tax': taxPrice,
'shipping': parseFloat(parseFloat(order.cargo_price).toFixed(2)),
'currency': currency,
'coupon': order.coupon,
'items': products
});
//For Track Ads conversions
if (GTAG_APP.params.GTAG_ADWORDS_ID !== '') {
gtag('event', 'conversion', {
'send_to': GTAG_APP.params.GTAG_ADWORDS_ID + '/' + GTAG_APP.params.GTAG_ADWORDS_LABEL,
'value': parseFloat(orderValue.toFixed(2)),
'currency': currency,
'transaction_id': order.transaction
});
}
});
},
beginCheckout: () => {
if (GTAG_APP.beginCheckoutButton) {
return;
}
GTAG_APP.beginCheckoutButton = true;
document.addEventListener('click', async function (event) {
if (!event.target.closest) return;
const button = event.target.closest('#addCartBtn, #cart-buy-btn, [data-toggle="link-checkout"]');
if (button) {
event.preventDefault();
try {
const cart = await GTAG_APP.getCartItems();
if (cart.items.length > 0) {
gtag("event", "begin_checkout", cart);
}
} catch (error) {
} finally {
let href = button.getAttribute('href');
if (!href || href === 'null' || href === '#') {
href = '/' + PAGE_LINK.ORDER || '/order';
}
window.location.href = href;
}
}
});
}
}
GTAG_APP.run();
Masaüstü bildirimlerine ekleyin.
Özel fırsatlardan ve güncel kampanyalardan haberiniz olsun ister misiniz?