Answer the question
In order to leave comments, you need to log in
How to find out the client id from google analytics on the first visit of a user?
I need to find out the client id from Google Analytics on the landing page in order to substitute this value in a hidden form field and then transfer it to CRM.
And I ran into an interesting problem. The first time the user logs in, the cookie is not read and the client_id is returned empty.
Google analytics is connected via GTM. I place the script code for reading cookies in the body of the page. I also tried to put it in GTM and run it after the analytics script worked. But it doesn't help much either...
What am I doing wrong? Where did the dog dig?
<script>
//берем client_id из кукисов аналитики.
function Gcid(){
// функция для получения cookie по имени
function getCookie(name){
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
// исходное значение cookie с именем _ga
var cidLong = getCookie('_ga');
// делим на части используя .
var tmp = cidLong.split('.');
// берем 3 и 4 фрагмент и склеиваем
var cid = tmp[2] + '.' + tmp[3];
// возвращаем значение
return cid;
}
//конец кода client_id из аналитики
alert (Gcid());
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question