Remove all traffic for a specific user agent

爱⌒轻易说出口 提交于 2019-12-06 03:54:58

问题


Is there a way to exclude, from all properties and all views of Google Analytics, the visitors with a specific user agent?

Note: it's not for spam/bot prevention (I already checked the feature Admin > View settings > Bot filtering > Exclude all hits from known bots and spiders), it's to remove a part of own traffic. I can't use IP filtering because my IP changes all the time, and I use many devices (mobile/desktop/laptop). I also can't use cookies, because often I want to test my website as a random non-logged user. I didn't find anything even after exploring deeply the Analytics UI. Maybe this requires API ?


回答1:


In the first step you have to get the User Agent into Google Analytics. You can do this with the Tag Manager, by creating a custom Javascipt. This can return the User Agent and you can send it to GA with a custom Dimension or a Event. (It´s also possible to do this without the GTM).

  1. Log in to the Tag Manager and navigate to Variables
  2. Now we have to add a Javascript Variable. The User Agent is stored in the navigator.userAgent property

  1. Now we have to push this data into the Google Analytics Account. We could do this with a custom Dimension or a Event. In this example we take a Event. As Action we send the Page Path - you don´t have to do this, but maybe it´s helpfull later for some reports. As a Trigger we define "All Pages" so the event is fired with every pageview. The User Agent we send as a Event Label.

If you have this information in GA you can add a filter on Account Level (for alle Views and Propertys). Filter Type = Custom > Select the Event or Dimension > Type the User Agent to exclude.

We can see the User Agent of the Users visited our site now in GA Behavior > Events > Top Events > Search for the Event Category (namend in Tag Manager (in our case "User Agent"))

In the last step we exclude the User Agent from all our Propertys and Views. Admin > All Filters (In the Account column) > "+ Add Filter" > Filter Type = Custom > Exclude > Select "Event Label" in the Dropdown > specify the User Agent to ignore

Select the Views, there this User Agent should be ignored > DONE




回答2:


(Google Tag Manager seemed a bit labyrinthic for me.)

I finally did this:

  • Use Custom UserAgent String extension (available for FF and Chrome) and set UserAgent to NoTracking (you can do it specifically for certain websites, i.e. your websites only, see options).

  • Add this in the PHP page, in the Analytics Javascript part:

    <?php  if ($_SERVER ['HTTP_USER_AGENT'] === 'NoTracking') echo 'if (false)'; ?>
    

    It looks like this:

    <script>
    <?php  if ($_SERVER ['HTTP_USER_AGENT'] === 'MyselfXYZ12') echo 'if (false)'; ?>
    (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','https://www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-xxxxxxx-x', 'auto');
    ga('send', 'pageview');
    <script>
    

    This will have the effect of disabling the creation of ga object for your traffic only.

NB: I first thought about disabling GA code for my own traffic via Javascript with if (navigator.userAgent == 'NoTracking') but it seems that the UserAgent change thanks to the extension "Custom UserAgent String" has effect only after the page is rendered, which is too late.



来源:https://stackoverflow.com/questions/44527385/remove-all-traffic-for-a-specific-user-agent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!