Analysis of the source of visitors using Sourcebuster.js or where the user came to the site from

Faced the other day with the library sourcebuster.js , I decided to tell a ready-made solution for determining the source of the visit.

I took information here . There is more detailed documentation. I will tell you a simple example of accounting and catching data.

1. Connection on the site :

In the article on the official website (the link to which is above) is given link to download the finished minified js file. It lies in the /dist directory. We will work with him. Installation via npm is also described there, but since this is a visual aid, we will omit it.

In the main template file, before the closing body tag, we connect and run the script.

<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
....

<!-- sbjs analytics -->
<script src="sourcebuster.min.js"></script>
<script>
sbjs.init();
</script>
<!-- END sbjs analytics -->

</body>
</html>

To check if the added script works, type in the browser console:

sbjs.get

No error should occur and an object with information should be returned.

console_sbjs

The information that is stored in sbjs.get.current is required for us to determine the source:

sbjs.get.current

2. Work on the server side :

Sourcebuster.js works with cookies . Therefore, up-to-date information will take effect only after the page is reloaded or the user visits any other link on the site (so that the page is reloaded and the cookie is updated).

Working with data in cookies is easy.

<?php

/**
* If there is no $_COOKIE['analytic_sbjs'] then return this
*/< br />function getTempInfo() {
    $a = strtr($_COOKIE['sbjs_current'], ['|||' => '&']);
    parse_str($a, $b);
    if (!$b) {
        $b = [
            'type' => 'organic',
            'src' => 'google',
            'mdm' => 'organic',
            'cmp' => '(none)',
            'cnt' => '(none)',
            'trm' => '(none)',
        ];
    }
    return (string) json_encode($b);
}

// Extract the stored information. If it is not present in analytical_sbjs, then pull getTempInfo()
$other = strip_tags(trim((string) $_COOKIE['analitic_sbjs']) ? : getTempInfo());

/ / json_decode, and lowercase everything for easier comparison
$decode = $other ? json_decode($other, true) : [];
$decode['src'] = mb_strtolower($decode['src']);
$decode['mdm'] = mb_strtolower($decode ['mdm']);

$source = 'Other';
if ($decode['src'] == 'google' && $decode['mdm' ] == 'cpc') {
    $source = 'Google Adwords';
} elseif ($decode['src'] == 'yandex' && $decode['mdm'] == 'cpc') {
&nbsp ;   $source = 'Yandex Direct';                
} elseif (mb_stripos($decode['src'], 'youtube') !== false) {
    $source = 'Youtube';                
} elseif (in_array($decode['src'], ['facebook', 'fb']) && in_array($decode['mdm'], ['cpc','ppc ','cpa','cpm','cpv','cpp'])) {
    $source = 'Facebook ad';                
} elseif ($decode['src'] == 'instagram' && in_array($decode['mdm'], ['cpc','ppc','cpa','cpm ','cpv','cpp'])) {
    $source = 'Instagram ad';                
} elseif (mb_stripos($decode['src'], 'google') !== false && $decode['mdm'] == 'organic') {
&nbsp ;   $source = 'Google SEO';                
} elseif (mb_stripos($decode['src'], 'yandex') !== false && $decode['mdm'] == 'organic') {
&nbsp ;   $source = 'SEO Yandex';                
} elseif ($decode['src'] == 'hotline' && $decode['mdm'] == 'cpc') {
    $source = 'hotline';                
} elseif ($decode['src'] == 'priceua' && $decode['mdm'] == 'cpc') {
    $source = 'Price';                
} elseif (mb_stripos($decode['src'], 'viber') !== false) {
    $source = 'Viber';                
} elseif ((mb_stripos($decode['src'], 'facebook') !== false || mb_stripos($decode['src'], 'fb') !== false) &amp ;& !in_array($decode['mdm'], ['cpc','ppc','cpa','cpm','cpv','cpp'])) {
     $source = 'Facebook other';                
} elseif ($decode['mdm'] == 'email') {
    $source = 'Email distribution';                
} elseif ($decode['mdm'] == 'organic') {
    $source = 'SEO other';                
} elseif ($decode['mdm'] == 'cpc') {
    $source = 'Advertising other';                
} elseif ($decode['src'] == 'nadavi') {
    $source = 'Ek';                
} elseif ($decode['src'] == 'sendpulse') {
    $source = 'Send Pulse';                
} elseif (mb_stripos($decode['src'], 'yandex') !== false) {
    $source = 'Yandex other';                
} elseif ($decode['mdm'] == 'referral') {
    $source = 'Referrals from other sites';                
} elseif ($decode['src'] == '(direct)' && $decode['mdm'] == '(none)') {
 &nbsp ;  $source = 'Direct hits';                
} elseif ($decode['mdm'] == 'display') {
    $source = 'Display';                
} else {
    $source = 'Other';
}
var_dump($source); // Information about the source.

As you can see, $source will contain information about the sources from which the client switched. It is advisable to use this method when placing an order or for a similar one-time action so that it is not on the first page of the user's login.

The information is stored in $_COOKIE['analytic_sbjs'] and in $_COOKIE['sbjs_current'] (but in a specific format). The getTempInfo() function is here as insurance, suddenly the user has cookies disabled or something went wrong (whether soap). In any case, this is approximate information, as it is based on js and cookies.

I gave an example without OOP to make it more clear. I think it's more important here to sort out what comes in src and mdm , and what it means in aggregate to determine the source.

This example does not show all the features of the linked library. The rest can be found in detail on the official website, the source is given above.

2341 2

Comments

Игорь, скорее всего так определяет сам sourcebuster. Я привел приблизительный скрипт по обработке на бэкенде этого всего, но можно переписать под себя согласно документации http://sbjs.rocks/#/usage.
Не могу разобраться почему скрипт определяет перехода из поиска Яндекса тип траффика как referral, а не organic. Изменить настройки скрипта не получается.
You have to log in to leave a comment. Sign in

Similar articles