簡體   English   中英

為什么我必須觸發兩次點擊事件才能觸發正確的數據層?

[英]Why do I have to trigger a click event twice to get the proper dataLayer to fire?

語境

我正在嘗試將事件推送到 Shopify 網站上的數據層。 我正在利用結合了液體和 JS 代碼的液體文件從站點中提取適當的數據以推送到數據層。 除了“從購物車中刪除”事件外,所有事件都正確觸發。

問題描述

最初的問題是“從購物車中刪除”事件數據層推送從未觸發,因為購物車頁面在推送之前重新加載以顯示更新的購物車。

使用下面的代碼,我現在可以觸發“從購物車中刪除”事件,但它僅在第二次單擊刪除圖標時觸發。

問題

在重新加載購物車之前,如何在首次單擊刪除圖標時觸發“從購物車中刪除”事件推送?

JS/液體助手文件

為簡潔起見,刪除了不必要的代碼。

if(!window.jQuery){
    var jqueryScript = document.createElement('script');
    jqueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js');
    document.head.appendChild(jqueryScript);
}

__DL__jQueryinterval = setInterval(function(){
    // wait for jQuery to load & run script after jQuery has loaded
    if(window.jQuery){
        
        /**********************
        * DYNAMIC DEPENDENCIES
        ***********************/

        __DL__ = {
            dynamicCart: true,  // if cart is dynamic (meaning no refresh on cart add) set to true
            debug: true, // if true, console messages will be displayed
            cart:  true,
            removeCart: true
        };

        customBindings = {
            cartTriggers: [],
            viewCart: [],
            removeCartTrigger: [],
        };

        /* DO NOT EDIT */
        defaultBindings = {
            //removeCartTrigger: ['[href*="/cart/change"]'],
            removeCartTrigger: ['.tests'],
        };

        // stitch bindings
        objectArray = customBindings;
        outputObject = __DL__;

        applyBindings = function(objectArray, outputObject){
            for (var x in objectArray) {
                var key = x;
                var objs = objectArray[x];
                values = [];
                if(objs.length > 0){
                    values.push(objs);
                    if(key in outputObject){
                        values.push(outputObject[key]);
                        outputObject[key] = values.join(", ");
                    }else{
                        outputObject[key] = values.join(", ");
                    }
                }
            }
        };

        applyBindings(customBindings, __DL__);
        applyBindings(defaultBindings, __DL__);

        /**********************
        * DATALAYER SECTIONS
        ***********************/

                /** DATALAYER: Cart View
                * Fire anytime a user views their cart (non-dynamic) */
                {% if template contains 'cart' %}
                var cart = {
                    'products':[{% for line_item in cart.items %}{
                        'id'       : {{line_item.product_id | json}},
                        'sku'      : {{line_item.sku | json}},
                        'variant'  : {{line_item.variant_id | json}},
                        'name'     : {{line_item.title | json}},
                        'price'    : {{line_item.price | money_without_currency | remove: "," | json}},
                        'quantity' : {{line_item.quantity | json}}
                    },{% endfor %}],
                    'pageType' : 'Cart',
                    'event'    : 'Cart'
                };

                dataLayer.push(cart);

                if(__DL__.debug){
                    console.log("Cart"+" :"+JSON.stringify(cart, null, " "));
                }

                __DL__.cart = cart.products;

                $(__DL__.removeCartTrigger).on('click', function (event) {
                    setTimeout(function(){
                       //const goTo = $(this).attr("data-href");  // store anchor href
                        // remove from cart
                        jQuery.getJSON("/cart", function (response) {
                            // get Json response
                            __DL__.removeCart = response;
                            var removeFromCart = {
                                'products': __DL__.removeCart.items.map(function (line_item) {
                                    return {
                                        'id'       : line_item.product_id,
                                        'sku'      : line_item.sku,
                                        'variant'  : line_item.variant_id,
                                        'name'     : line_item.title,
                                        'price'    : (line_item.price/100),
                                        'quantity' : line_item.quantity
                                    }
                                }),
                                'pageType' : 'Remove from Cart',
                                'event'    : 'Remove from Cart'
                            };
                            __DL__.removeCart = removeFromCart;

                            var cartIDs = [];
                            var removeIDs = [];
                            var removeCart = [];

                            // remove from cart logic
                            for(var i=__DL__.cart.length-1;i>=0;i--){var x=parseFloat(__DL__.cart[i].variant);cartIDs.push(x)}for(var i=__DL__.removeCart.products.length-1;i>=0;i--){var x=parseFloat(__DL__.removeCart.products[i].variant);removeIDs.push(x)}function arr_diff(b,c){var a=[],diff=[];for(var i=0;i<b.length;i++){a[b[i]]=true}for(var i=0;i<c.length;i++){if(a[c[i]]){delete a[c[i]]}else{a[c[i]]=true}}for(var k in a){diff.push(k)}return diff};var x=arr_diff(cartIDs,removeIDs)[0];for(var i=__DL__.cart.length-1;i>=0;i--){if(__DL__.cart[i].variant==x){removeCart.push(__DL__.cart[i])}}

                            dataLayer.push(removeCart);

                            if (__DL__.debug) {
                                console.log("Item Removed from Cart"+" :"+JSON.stringify(removeCart, null, " "));
                            }
                        });
                    }, 2000);
                });

                {% endif %}

                            /**********************
                            * DATALAYER EVENT BINDINGS
                            ***********************/

                            /** DATALAYER:
                            * Add to Cart / Dynamic Cart View
                            * Fire all pages trigger after all additional dataLayers have loaded. */

                            $(document).ready(function() {

                                /** DATALAYER: Cart */

                                // stage cart data
                                function mapJSONcartData(){
                                    jQuery.getJSON('/cart.js', function (response) {
                                        // get Json response
                                        __DL__.cart = response;
                                        var cart = {
                                            'products': __DL__.cart.items.map(function (line_item) {
                                                return {
                                                    'id'       : line_item.id,
                                                    'sku'      : line_item.sku,
                                                    'variant'  : line_item.variant_id,
                                                    'name'     : line_item.title,
                                                    'price'    : (line_item.price/100),
                                                    'quantity' : line_item.quantity
                                                }
                                            }),
                                            'pageType' : 'Cart',
                                            'event'    : 'Cart'
                                        };
                                        if(cart.products.length > 0){
                                            dataLayer.push(cart);
                                            if (__DL__.debug) {
                                                console.log("Cart"+" :"+JSON.stringify(cart, null, " "));
                                            }
                                        }
                                    });
                                }

                                viewcartfire = 0;

                                // view cart
                                $(__DL__.viewCart).on('click', function (event) {
                                    if(viewcartfire !== 1){
                                        viewcartfire = 1;
                                        // if dynamic cart is TRUE
                                        if (__DL__.dynamicCart) {
                                            cartCheck = setInterval(function () {
                                                // begin check interval
                                                if ($(__DL__.cartVisableSelector).length > 0) {
                                                    // check visible selectors
                                                    clearInterval(cartCheck);
                                                    mapJSONcartData();
                                                    $(__DL__.removeCartTrigger).on('click', function (event) {
                                                        // remove from cart
                                                        var link = $(this).attr("href");
                                                        jQuery.getJSON(link, function (response) {
                                                            // get Json response
                                                            __DL__.removeCart = response;
                                                            var removeFromCart = {
                                                                'products': __DL__.removeCart.items.map(function (line_item) {
                                                                    return {
                                                                        'id'       : line_item.id,
                                                                        'sku'      : line_item.sku,
                                                                        'variant'  : line_item.variant_id,
                                                                        'name'     : line_item.title,
                                                                        'price'    : (line_item.price/100),
                                                                        'quantity' : line_item.quantity
                                                                    }
                                                                }),
                                                                'pageType' : 'Remove from Cart',
                                                                'event'    : 'Remove from Cart'
                                                            };
                                                            dataLayer.push(removeFromCart);
                                                            if (__DL__.debug) {
                                                                console.log("Cart"+" :"+JSON.stringify(removeFromCart, null, " "));
                                                            }
                                                        });
                                                    });
                                                }
                                            }, 500);
                                        }
                                    }
                                });

                                // add to cart
                                jQuery.getJSON('/cart.js', function (response) {
                                    // get Json response
                                    __DL__.cart = response;
                                    var cart = {
                                        'products': __DL__.cart.items.map(function (line_item) {
                                            return {
                                                'id'       : line_item.id,
                                                'sku'      : line_item.sku,
                                                'variant'  : line_item.variant_id,
                                                'name'     : line_item.title,
                                                'price'    : (line_item.price/100),
                                                'quantity' : line_item.quantity
                                            }
                                        })
                                    }
                                    __DL__.cart = cart;
                                    collection_cartIDs = [];
                                    collection_matchIDs = [];
                                    collection_addtocart = [];
                                    for (var i = __DL__.cart.products.length - 1; i >= 0; i--) {
                                        var x = parseFloat(__DL__.cart.products[i].variant);
                                        collection_cartIDs.push(x);
                                    }
                                });

                                function __DL__addtocart(){



                                    dataLayer.push(product, {
                                        'pageType' : 'Add to Cart',
                                        'event'    : 'Add to Cart'
                                    });

                                    if (__DL__.debug) {
                                        console.log("Add to Cart"+" :"+JSON.stringify(product, null, " "));
                                    }

                                    // if dynamic cart is TRUE
                                    if (__DL__.dynamicCart) {
                                        console.log("dynamic");
                                        var cartCheck = setInterval(function () {
                                            // begin check interval
                                            if ($(__DL__.cartVisableSelector).length > 0) {
                                                // check visible selectors
                                                clearInterval(cartCheck);
                                                mapJSONcartData();
                                                $(__DL__.removeCartTrigger).on('click', function (event) {
                                                    // remove from cart
                                                    var link = $(this).attr("href");
                                                    jQuery.getJSON(link, function (response) {
                                                        // get Json response
                                                        __DL__.removeCart = response;
                                                        var removeFromCart = {
                                                            'products': __DL__.removeCart.items.map(function (line_item) {
                                                                return {
                                                                    'id'       : line_item.id,
                                                                    'sku'      : line_item.sku,
                                                                    'variant'  : line_item.variant_id,
                                                                    'name'     : line_item.title,
                                                                    'price'    : (line_item.price/100),
                                                                    'quantity' : line_item.quantity
                                                                }
                                                            }),
                                                            'pageType' : 'Remove from Cart',
                                                            'event'    : 'Remove from Cart'
                                                        };
                                                        dataLayer.push(removeFromCart);
                                                        if (__DL__.debug) {
                                                            console.log("Cart"+" :"+JSON.stringify(removeFromCart, null, " "));
                                                        }
                                                    });
                                                });
                                            }
                                        }, 500);
                                    }
                                }

                                $(document).on('click', __DL__.cartTriggers, function() {
                                    __DL__addtocart();
                                });

                                }); // document ready
                            }
                        }, 500);

從購物車鏈接中刪除

<a href="#" data-href="/cart/change?line=1&amp;quantity=0" class="tests cart-item__remove icon-fallback">
    <i class="icon icon--close" aria-hidden="true"> </i>
    <span class="icon-fallback__text">Remove</span>
</a>

初始數據層推送(第一次從購物車鏈接中刪除)

{
  event: "Cart",
  gtm: {uniqueEventId: XXX, start: XXXXXXX},
  logState: "Logged Out",
  firstLog: false,
  customerEmail: null,
  timestamp: 1626795802651,
  currency: "USD",
  pageType: "Cart",
  products: [
    {
      id: XXXX,
      sku: "XXX",
      variant: XXX,
      name: "XXXX",
      price: 20,
      quantity: 1
    }
  ],
  necessaryTags: true,
  performanceTags: true,
  functionalTags: true,
  targettingTags: true
}

第二個數據層推送(第二次從購物車鏈接中刪除)

{
  event: "Remove from Cart",
  gtm: {uniqueEventId: XXX, start: XXXXXX},
  logState: "Logged Out",
  firstLog: false,
  timestamp: 1626796193806,
  currency: "USD",
  pageType: "Remove from Cart",
  products: [
    {
      id: XXXX,
      sku: "XXX",
      variant: XXX,
      name: "XXXX",
      price: 20,
      quantity: 1
    }
  ],
  necessaryTags: true,
  performanceTags: false,
  functionalTags: false,
  targettingTags: false
}

我提前感謝您的幫助!

事實證明,有一個 3rd Party Shopify 應用程序劫持了購物車表單提交事件並強制重新加載購物車頁面。 雖然這不能解決我的問題,但它確實回答了這篇文章中的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM