VKirill

Untitled

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