每个会话只发送一次事件? [英] Send event only once per session?

查看:136
本文介绍了每个会话只发送一次事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次会话只能发送一次Google Analytics(分析)事件吗?我不想在每个网页浏览中发送我的具体事件。



我想达到的目的:

我目前正在运行一个没有任何广告的网站。我想测试一下我的网站有多少访问者在使用adblockers。



我会添加一个元素,如

 < div class =ad>< / div> 

这可能会被每个adblocker(至少大多数)所滤除。然后,我只是测试与jQuery的,如果元素存在

  $(function(){
function test(){
var len = $('。ad')。length;
if(len === 0){
ga.send(..);
}
}

//给adblockers一秒钟以删除所有内容,以防万一
setTimeout(test,500);

});






我可以在自定义Cookie中使用一种解决方法,像

  //简化
if(!cookie){
test();
}

但是我会感兴趣的是, Google Analytics(分析)事件是一种热门类型(如综合浏览量),不能将其范围限定在会话或用户范围内。即使您只能在每个会话中发送一个事件(使用Cookie或sessionStorage,您实际上也不会在您的GA报告中获得全部图片。)

为了提供更好的服务解决方案中,考虑使用一个范围为会话的自定义维度来存储这些数据,最关键的是该维度适用于范围内的所有匹配类型 - 这意味着任何浏览量,事件,等等也有与之相关的维度。



为此,请在Google Analytics管理员的Custom Properties下的属性设置下创建维度。字符串,看起来像'dimension1''dimension 4'),然后粘贴 BEFORE 发送综合浏览量(理想情况下)或事件。



请记住:尺寸不符合要求,因此只需设置它们,也发送!另外,请务必启用非互动(如果这是您选择的路线),否则您将错误地报告使用Ad (注意:如果您要将setTimeout作为检测的一部分,请不要使用此方法,因为它会延迟页面浏览发送的时间。){/ b> ... 考虑一种不需要超时的方法。):

 < script> ; 
(function(i,s,o,g,r,a,m){i ['GoogleAnalyticsObject'] = r; i [r] = i [r] || function(){
(i [r] .q = i [r] .q || [])。push(arguments)},i [r] .l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a,m)
})(window,document,'script' , '// www.google-analytics.com/analytics.js','ga');

ga('create','UA-XXXXX-Y','auto');

//检测到此处。如果为true:
ga('set','dimension1','Ad Blocker'); //将'dimension1'更改为你的。

ga('send','pageview');
< / script>

如果您在<$ c之前无法执行检测,则上面的代码看起来像这样
$ b

  function test(){$ b $> $ c $< head>  GA发送pageview片段。 $ b var len = $('。ad')。length; 
if(len === 0){
ga('set','dimension1','Ad Blocker'); //将'dimension1'更改为你的。
ga('send','event','Ad Blocker',{nonInteraction:true}); //根据需要添加操作和标签。
}
}


Is it possible to send a google analytics event only once per session? I don't want to send my specific event with every pageview.

What I want to achieve:

I'm running a website without any ads at the moment. And I want to test how many visitors of my website are using adblockers.

I would add an element like

<div class="ad"></div>

which would be filtered out by probably every adblocker (at least most of them). Then I just would test with jquery if the element exists

$(function(){
     function test(){
         var len = $('.ad').length;
         if(len === 0){
             ga.send(..);
         }
     }

    // give adblockers a sec to remove everything, just in case
    setTimeout(test,500);

});


I could probably use a workaround with a custom cookie, like

// simplified
if (!cookie) {
    test();
}

But I'd be interested if this is possible with google api only

解决方案

Google Analytics events are a hit type (like pageviews) which can not be scoped to sessions or users. Even if you could only send one event per session (using either cookies or sessionStorage, you wouldn't actually get the whole picture in your GA reports.

To offer a better solution, consider using a Custom Dimension with a scope of "session" to store that data. What's cool about this is that the dimension is applied to all hit types within the scope- meaning any pageview, event, etc. also has the dimension associated with it.

To do this, create the dimension in Google Analytics Admin under the Property settings within Custom Definitions. Get the index number (a string that looks like 'dimension1' or 'dimension 4') and paste the line BEFORE sending the pageview (ideally) or an event.

Remember: Dimensions are not hits, so simply setting them does nothing until a hit is also sent! Also, be sure to enable non-interaction on the events (if that is the route you choose) or else you will be misreporting bounces from those using Ad Blockers.

Here is the ideal snippet (Note: if you are going to setTimeout as part of your detection, do not use this method as it would delay the pageview send too... Consider a method that doesn't need a timeout.):

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');

//Detection goes here. If true:
ga('set', 'dimension1', 'Ad Blocker'); //Change 'dimension1' to yours.

ga('send', 'pageview');
</script>

Your code from above would look like this if you can't do the detection before the <head> GA send pageview snippet.

function test(){
     var len = $('.ad').length;
     if(len === 0){
         ga('set', 'dimension1', 'Ad Blocker'); //Change 'dimension1' to yours.
         ga('send', 'event', 'Ad Blocker', {nonInteraction: true}); //Add action and label as needed.
     }
 }

这篇关于每个会话只发送一次事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆