📜 ⬆️ ⬇️

Referrer spam in google analytics and how to deal with it



Recently, a joyful client approached me with a question, he said, on the site that had just started, traffic from referrers began to appear, except that it was unclear what kind of sites he had passed through them, and there - El Zilcho.
It turns out that this is such a new youth-modern type of spam - “Referrer Spam”.
Carefully, under the cut of the picture, google analytics and a little bydlokod in php.

This type of spam is relevant, of course, not only for google analytics, just cite it as an example. So, what is it:


')
All of these referrers are not real, Chinese fake. They interfere with statistics and give false hope to honest people.
You can ban them anywhere on the server, in code, firewall, or in the web server configurations.
In nginx, this is done very simply, the documentation is: wiki.nginx.org/Referrer_Spam_Blocking
It remains to finish our bad referrers and ready. Beautiful, but boring, we are not looking for easy ways, and then it will be hard for the client to change it himself.
GA allows you to filter analytics by various parameters, you can do it by hand as follows:



Hands too boring. It will have to be done for all clients, for all sites. Let's strike with automation on sloppiness, we will write a script that will do it.

First you need to register the project in the console with Google at: console.developers.google.com/project .





Next, turn on the Analytics API





We need the user's permission, so go to the consent screen, select the email and write the name. It is important that this step be taken before generating the id and secret. Next, generate the Client ID:







Well, finally you can get to the code. Save the pieces from the demonstrations and manulals, the most important:

$details = new Google_Service_Analytics_FilterExpression(); $details->setField('CAMPAIGN_SOURCE'); $details->setMatchType('MATCHES'); $details->setExpressionValue('  ,   '); $details->setCaseSensitive(false); $filter->setExcludeDetails($details); $analytics->management_filters->insert($accountId, $filter); 

An interesting feature of GA is that a rehex with domains cannot be longer than 255 characters, therefore, if it is suddenly longer, then it is necessary to split it into several.
The full code on the githaba , we enter our keys, authorize it with a Google account and is ready.

All that remains for us to do is to choose in which representations we will filter (I filter everywhere, although some do not recommend it). To do this, go to google analytics, select an account, Admin -> All filters -> Exclude Referrer Spam # ... -> Apply Filter to Views -> select the necessary twigs that we want to filter and wait, unfortunately the filters do not immediately apply, but in the future they will not appear.

Source: https://habr.com/ru/post/257767/


All Articles