Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- // Функция для извлечения параметра из URL
- function getUrlParameter(name) {
- name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
- var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
- var results = regex.exec(location.search);
- return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
- }
- // Функция для извлечения значения из utm_content
- function extractSourceFromUtmContent(utmContent) {
- var match = utmContent.match(/\|source:([^|]+)\|/);
- return match ? match[1] : null;
- }
- // Функция, которая будет выполняться после загрузки Яндекс.Метрики
- function afterYandexMetricaLoaded() {
- var yaID;
- // Получение Client ID
- ym(88964813, 'getClientID', function(clientID) {
- yaID = clientID;
- console.log("ClientID", yaID);
- // Получение значения source из utm_content
- var utmContent = getUrlParameter('utm_content');
- var source = extractSourceFromUtmContent(utmContent);
- console.log("Source", source);
- // Получение IP-адреса
- var xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://api.ipify.org?format=json', true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4 && xhr.status == 200) {
- var response = JSON.parse(xhr.responseText);
- var ipAddress = response.ip;
- console.log("IP Address", ipAddress);
- // Отправка IP-адреса и source на сервер как параметры
- ym(88964813, "params", {
- clientID: yaID,
- ipAddress: ipAddress,
- source: source,
- });
- }
- };
- xhr.send();
- });
- }
- // Подписываемся на событие загрузки Яндекс.Метрики
- document.addEventListener('DOMContentLoaded', afterYandexMetricaLoaded);
- </script>
Advertisement
Add Comment
Please, Sign In to add comment