Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- function pushCustomGAEvent(eventType, eventElementType, elementId) {
- if (window.preDataLayer) {
- window.preDataLayer.push({
- event: "newEvent",
- newEventData: {
- type: eventType,
- elementType: eventElementType,
- elementId: elementId
- }
- });
- }
- }
- function pushImpretionEvent() {
- window.preDataLayer.push({
- event: "newEvent",
- newEventData: {
- ecommerce: {
- impressions: {
- "Adoric - Upsale: NEW gass in the cart": [
- {
- id: PRODUCT_SKU,
- title: "Spare Cylinder",
- type: "Carbonator",
- price: 2999,
- available: true,
- variants: [
- {
- id: PDRODUCT_ID,
- name: "Spare Cylinder",
- price: 2999,
- available: true,
- productId: PRODUCT_SKU
- }
- ]
- }
- ]
- }
- },
- type: "ecommerceData"
- }
- });
- }
- function pushAddToCartEvent() {
- window.preDataLayer = window.dataLayer || [];
- window.preDataLayer.push({
- event: "newEvent",
- newEventData: {
- ecommerce: {
- add: {
- list: "Adoric - Upsale: NEW gass in the cart",
- products: [{ variant_id: PDRODUCT_ID, quantity: 1 }]
- }
- },
- type: "ecommerceData"
- }
- });
- }
- var GA_EVENT_CATEGORY = "Adoric - GASINTHECART";
- var GA_EVENT_ACTION = "Added to the cart";
- var GA_EVENT_LABEL = "Carbonating Cylinder ";
- var PDRODUCT_ID = 39539279659050;
- var PRODUCT_SKU = 6615783702570;
- pushImpretionEvent();
- function adoricAddToCart(productId) {
- var data = { id: productId, quantity: 1 };
- var xhr = new XMLHttpRequest();
- xhr.withCredentials = true;
- xhr.addEventListener("readystatechange", function () {
- if (this.readyState === 4) {
- console.log(this.responseText);
- pushCustomGAEvent(GA_EVENT_CATEGORY, GA_EVENT_ACTION, GA_EVENT_LABEL);
- pushAddToCartEvent();
- /*window.location.reload();*/
- }
- });
- xhr.open("POST", window.location.origin + "/cart/add.js");
- xhr.setRequestHeader("content-type", "application/json");
- xhr.setRequestHeader("cache-control", "no-cache");
- xhr.send(JSON.stringify(data));
- }
- adoricAddToCart(PDRODUCT_ID);
- })();
Add Comment
Please, Sign In to add comment