Last active
June 10, 2024 21:12
-
-
Save molavec/a4404319faea31b2a7dbdc246562e788 to your computer and use it in GitHub Desktop.
GTM DataLayer Events Functions
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
type WindowWithDataLayer = Window & { | |
dataLayer: Record<string, any>[]; | |
}; | |
declare const window: WindowWithDataLayer; | |
export const pageview = (url: string) => { | |
if (typeof window.dataLayer !== 'undefined') { | |
window.dataLayer.push({ | |
event: 'pageview', | |
page: url, | |
}); | |
} else { | |
// console.log({ | |
// event: 'pageview', | |
// page: url, | |
// }); | |
} | |
}; | |
export const purchase = (price: number, orderId: string) => { | |
if (typeof window.dataLayer !== 'undefined') { | |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. | |
dataLayer.push({ | |
event: "purchase", | |
ecommerce: { | |
transaction_id: orderId, | |
currency: "CLP", | |
value: price, | |
items: [ | |
{ | |
item_id: "01", | |
item_name: "Consulta Online", | |
item_brand: "Dermacne", | |
price: price, | |
quantity: 1 | |
} | |
] | |
} | |
}); | |
} else { | |
// console.log({ | |
// event: 'pageview', | |
// page: url, | |
// }); | |
} | |
}; | |
export const viewItem = (price: number, itemId: string, itemName: string) => { | |
if (typeof window.dataLayer !== 'undefined') { | |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. | |
dataLayer.push({ | |
event: "view_item", | |
ecommerce: { | |
currency: "CLP", | |
value: price, | |
items: [ | |
{ | |
item_id: itemId, | |
item_name: itemName, | |
price: price, | |
quantity: 1 | |
} | |
] | |
} | |
}); | |
} else { | |
// console.log({ | |
// event: 'pageview', | |
// page: url, | |
// }); | |
} | |
}; | |
export const addToCart = (price: number, itemId: string, itemName: string) => { | |
if (typeof window.dataLayer !== 'undefined') { | |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. | |
dataLayer.push({ | |
event: "add_to_cart", | |
ecommerce: { | |
currency: "CLP", | |
value: price, | |
items: [ | |
{ | |
item_id: itemId, | |
item_name: itemName, | |
price: price, | |
quantity: 1 | |
} | |
] | |
} | |
}); | |
} else { | |
// console.log({ | |
// event: 'pageview', | |
// page: url, | |
// }); | |
} | |
}; | |
export const beginCheckout = (price: number, itemId: string, itemName: string) => { | |
if (typeof window.dataLayer !== 'undefined') { | |
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. | |
dataLayer.push({ | |
event: "begin_checkout", | |
ecommerce: { | |
currency: "CLP", | |
value: price, | |
items: [ | |
{ | |
item_id: itemId, | |
item_name: itemName, | |
price: price, | |
quantity: 1 | |
} | |
] | |
} | |
}); | |
} else { | |
// console.log({ | |
// event: 'pageview', | |
// page: url, | |
// }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment