简体   繁体   English

使用Google跟踪代码管理器跟踪滑动事件

[英]Tracking swipe events with Google Tag Manager

I have a client who wishes to track swipe events (swipe left, swipe right) on a FlexSlider photo gallery. 我有一个客户希望在FlexSlider照片库上跟踪滑动事件(向左滑动,向右滑动)。 I am using a small script to detect swipe events, and it works quite well to send an alert() or console.log() for testing purposes. 我正在使用一个小的脚本来检测滑动事件,并且可以很好地发送alert()console.log()进行测试。 However, when I tried to instead push an event to Google Tag Manager it doesn't appear to be sent. 但是,当我尝试将事件推送到Google跟踪代码管理器时,似乎未发送该事件。

Here is how I am attempting to track the events: 这是我尝试跟踪事件的方式:

// Previous Photo
jQuery('#photo_gallery').on('swiperight', 'img', function() {
    dataLayer.push({'category': 'photo-gallery', 'action' : 'photo-gallery-previous', 'label' : 'previous'});
});

// Next Photo           
jQuery('#photo_gallery').on('swipeleft', 'img', function() {
    dataLayer.push({'category': 'photo-gallery', 'action' : 'photo-gallery-next', 'label' : 'next'});
});

Where #photo_gallery is the ID of the standard <div class="flexslider"> container. 其中#photo_gallery是标准<div class="flexslider">容器的ID。

Here is the script I am using to create the swipeleft swiperight events: 这是我用来创建swipeleft swiperight事件的脚本:

(function($) {

$.detectSwipe = {
    enabled: 'ontouchstart' in document.documentElement,
    preventDefault: true,
    threshold: 20
};

var startX,
    startY,
    isMoving = false;

function onTouchEnd() {
    this.removeEventListener('touchmove', onTouchMove);
    this.removeEventListener('touchend', onTouchEnd);
    isMoving = false;
}

function onTouchMove(e) {
    if ($.detectSwipe.preventDefault) { e.preventDefault(); }
    if(isMoving) {
        var x = e.touches[0].pageX;
        var y = e.touches[0].pageY;
        var dx = startX - x;
        var dy = startY - y;
        var dir;
        if(Math.abs(dx) >= $.detectSwipe.threshold) {
            dir = dx > 0 ? 'left' : 'right'
        } else if(Math.abs(dy) >= $.detectSwipe.threshold) {
            dir = dy > 0 ? 'down' : 'up'
        }
        if(dir) {
            onTouchEnd.call(this);
            $(this).trigger('swipe', dir).trigger('swipe' + dir);
        }
    }
}

function onTouchStart(e) {
    if (e.touches.length == 1) {
        startX = e.touches[0].pageX;
        startY = e.touches[0].pageY;
        isMoving = true;
        this.addEventListener('touchmove', onTouchMove, false);
        this.addEventListener('touchend', onTouchEnd, false);
    }
}

function setup() {
    this.addEventListener && this.addEventListener('touchstart', onTouchStart, false);
}

function teardown() {
    this.removeEventListener('touchstart', onTouchStart);
}

$.event.special.swipe = { setup: setup };

$.each(['left', 'up', 'down', 'right'], function () {
    $.event.special['swipe' + this] = { setup: function(){
        $(this).on('swipe', $.noop);
    } };
});

})(jQuery);

Note: The above script works for console logs and alerts 注意:以上脚本适用于控制台日志和警报

Does anyone have any experience tracking swipe events in Google Analytics/Tag Manager? 是否有人在Google Analytics(分析)/跟踪代码管理器中跟踪滑动事件有经验? It would be nice to tap into the FlexSlider built in swipe functionality, but I wouldn't want to modify any of the plugin code. 最好使用内置的滑动功能FlexSlider,但我不想修改任何插件代码。

You should also include an "event" parameter (of, say, "swipe") to use in your GTM trigger: 您还应该在GTM触发器中包括一个“事件”参数(例如“刷卡”):

dataLayer.push({
   'event': 'swipe',
   // your other parameters
})

Quoting from this: https://developers.google.com/tag-manager/devguide?hl=en 引用如下: https : //developers.google.com/tag-manager/devguide?hl= zh-CN

Google Tag Manager provides a special data layer variable called an event that is used within JavaScript event listeners to initiate tag firing when a user interacts with website elements such as a button. Google跟踪代码管理器提供了一个称为事件的特殊数据层变量,当用户与网站元素(例如按钮)进行交互时,该事件会在JavaScript事件监听器中用于启动代码触发。

You could then use the 'swipe' event to fire your tags. 然后,您可以使用'swipe'事件触发代码。

Not sure if it is still of interest, but as an alternative to dataLayer (best option imho) you may create a "History Change" Trigger. 不知道它是否仍然令人感兴趣,但是可以替代dataLayer(最佳选择imho)来创建“历史记录更改”触发器。

"Triggers based on the History Change event will fire a tag when the URL fragment (hash) changes or when a site is using the HTML5 pushstate APIs. This trigger is useful to fire tags tracking virtual pageview in an Ajax application, for instance." “例如,当URL片段(哈希)更改或网站使用HTML5推送状态API时,基于“历史记录更改”事件的触发器将触发标签。例如,此触发器对于在Ajax应用程序中触发跟踪虚拟综合浏览量的标签很有用。” https://support.google.com/tagmanager/answer/6106961?hl=en https://support.google.com/tagmanager/answer/6106961?hl=zh_CN

trigger screenshot 触发萤幕撷取画面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM