簡體   English   中英

填充 Google 標簽管理器數據層的問題

[英]Problems Populating the Google Tag Manager datalayer

在過去的幾天里,當單擊“添加到購物車”按鈕時,我一直試圖在 OpenCart 產品頁面上填充 Google 標簽管理器數據層。

我知道我需要使用 push 方法,但我不知道在哪里嵌套它,因為我沒有 HTML 按鈕,只有一個 JavaScript 函數。

功能:

 <script type="text/javascript"><!-- $('#button-cart').on('click', function() { $.ajax({ url: 'index.php?route=checkout/cart/add', type: 'post', data: $('#product input[type=\\'text\\'], #product input[type=\\'hidden\\'], #product input[type=\\'radio\\']:checked, #product input[type=\\'checkbox\\']:checked, #product select, #product textarea'), dataType: 'json', beforeSend: function() { $('#button-cart').button('loading'); }, complete: function() { $('#button-cart').button('reset'); }, success: function(json) { $('.alert, .text-danger').remove(); $('.form-group').removeClass('has-error'); if (json['error']) { if (json['error']['option']) { for (i in json['error']['option']) { var element = $('#input-option' + i.replace('_', '-')); if (element.parent().hasClass('input-group')) { element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>'); } else { element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>'); } } } if (json['error']['recurring']) { $('select[name=\\'recurring_id\\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>'); } // Highlight any found errors $('.text-danger').parent().addClass('has-error'); } if (json['success']) { $('.breadcrumb').after('<div class="alert alert-success">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>'); $('#cart > button').html('<i class="fa fa-shopping-cart"></i> ' + json['total']); $('html, body').animate({ scrollTop: 0 }, 'slow'); $('#cart > ul').load('index.php?route=common/cart/info ul li'); } }, error: function(xhr, ajaxOptions, thrownError) { alert(thrownError + "\\r\\n" + xhr.statusText + "\\r\\n" + xhr.responseText); } }); });

我把互聯網顛倒了,但沒有找到解決辦法。 任何幫助表示贊賞。

在您問題的代碼中,您已經為“添加到購物車”按鈕初始化了點擊處理程序。 將您的 dataLayer 邏輯放在單擊處理程序中。

$('#button-cart').on('click', function() {
    window.dataLayer = window.dataLayer || [];
    dataLayer.push({
        'event': 'button-add-to-cart-click'
    });
    // other code
});

暫無
暫無

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

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