VKirill

Untitled

Jun 20th, 2024
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.   // Функция для извлечения параметра из URL
  3.   function getUrlParameter(name) {
  4.     name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  5.     var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
  6.     var results = regex.exec(location.search);
  7.     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  8.   }
  9.  
  10.   // Функция для извлечения значения из utm_content
  11.   function extractSourceFromUtmContent(utmContent) {
  12.     var match = utmContent.match(/\|source:([^|]+)\|/);
  13.     return match ? match[1] : null;
  14.   }
  15.  
  16.   // Функция, которая будет выполняться после загрузки Яндекс.Метрики
  17.   function afterYandexMetricaLoaded() {
  18.     var yaID;
  19.     // Получение Client ID
  20.     ym(88964813, 'getClientID', function(clientID) {
  21.       yaID = clientID;
  22.       console.log("ClientID", yaID);
  23.  
  24.       // Получение значения source из utm_content
  25.       var utmContent = getUrlParameter('utm_content');
  26.       var source = extractSourceFromUtmContent(utmContent);
  27.       console.log("Source", source);
  28.  
  29.       // Получение IP-адреса
  30.       var xhr = new XMLHttpRequest();
  31.       xhr.open('GET', 'https://api.ipify.org?format=json', true);
  32.       xhr.onreadystatechange = function() {
  33.         if (xhr.readyState == 4 && xhr.status == 200) {
  34.           var response = JSON.parse(xhr.responseText);
  35.           var ipAddress = response.ip;
  36.           console.log("IP Address", ipAddress);
  37.  
  38.           // Отправка IP-адреса и source на сервер как параметры
  39.           ym(88964813, "params", {
  40.             clientID: yaID,
  41.             ipAddress: ipAddress,
  42.             source: source,
  43.           });
  44.         }
  45.       };
  46.       xhr.send();
  47.     });
  48.   }
  49.  
  50.   // Подписываемся на событие загрузки Яндекс.Метрики
  51.   document.addEventListener('DOMContentLoaded', afterYandexMetricaLoaded);
  52. </script>
  53.  
Advertisement
Add Comment
Please, Sign In to add comment