S
S
soofftt912014-10-30 04:01:24
JSON
soofftt91, 2014-10-30 04:01:24

How to pass visit parameters (json) to Yandex in a running script?

Good day to all. In js, the experience is not at all great, at first glance the task seemed trifling, but it raced all day.
I do a/b testing for a client using js. I generate a random number, depending on the number, I manipulate the element on the page, simultaneously writing it in cookies so that when I visit it again, I show the same option. Everything works like clockwork.
To track the results of an a/b test, you need to pass the visit parameters to Yandex, this is the problem.
Code that checks cookies or generated number:

if (getCookie('ab_test')==null) {
if(rand == 1) {
var yaParams = {likes: "variant_1"};
setCookie('ab_test', '1', 1000*60*60*30);
} else if(rand == 2) {
var yaParams = {likes: "variant_2"};
setCookie('ab_test', '2', 1000*60*60*30);
setTimeout(likeUp, 5000);
}
} else if(getCookie('ab_test') == '1') {
var yaParams = {likes: "variant_1"};
} else if(getCookie('ab_test') == '2') {
var yaParams = {likes: "variant_2"};
setTimeout(likeUp, 5000);
}

var yaParams = {likes: "variant_1"}; - this line should pass the parameters of the visit.
If you take this line separately, then the parameter is passed, but in this case it is of no use:
if(random >= '0.5') {var rand = 1;} else {var rand = 2;};
if (getCookie('ab_test')==null) {
if(rand == 1) {
setCookie('ab_test', '1', 1000*60*60*30);
} else if(rand == 2) {
setCookie('ab_test', '2', 1000*60*60*30);
setTimeout(likeUp, 5000);
}
} else if(getCookie('ab_test') == '1') {
} else if(getCookie('ab_test') == '2') {
setTimeout(likeUp, 5000);
}
var yaParams = {likes: "variant_1"};

Please tell me what I'm doing wrong.
UPD . Just in case, here's the whole code:
function setCookie(c_name,value,exseconds){
var exdate=new Date();
exdate.setTime(exdate.getTime() + exseconds);
var c_value=escape(value);
document.cookie=c_name + "=" + c_value + ((exseconds==null) ? "" : "; expires="+exdate.toUTCString()) + "; path=/";
}

function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}

function likeUp() {
//получаем кол-во шаров
var vkCount = $('.b-share-btn__vkontakte span.b-share-counter').html();
var fbCount = $('.b-share-btn__facebook span.b-share-counter').html();
var twCount = $('.b-share-btn__twitter span.b-share-counter').html();
var odnCount = $('.b-share-btn__odnoklassniki span.b-share-counter').html();
var moimirCount = $('.b-share-btn__moimir span.b-share-counter').html();
var gglCount = $('.b-share-btn__gplus span.b-share-counter').html();

if(moimirCount == '') {moimirCount = '1';} 
$('.b-share-btn__wrap a').addClass('b-share-btn__counter');

// переводим значение в числовое
vkCount = parseInt(vkCount);
fbCount = parseInt(fbCount);
twCount = parseInt(twCount);
odnCount = parseInt(odnCount);
moimirCount = parseInt(moimirCount);
gglCount = parseInt(gglCount);
var summa = vkCount+fbCount+twCount+moimirCount+moimirCount+gglCount;
summa = parseInt(summa);
summa = summa*2;

// увеличиваем значение
$('.b-share-btn__vkontakte span.b-share-counter').html(vkCount + 75 + summa);
$('.b-share-btn__facebook span.b-share-counter').html(fbCount + 137 + summa);
$('.b-share-btn__twitter span.b-share-counter').html(twCount + 28 + summa);
$('.b-share-btn__odnoklassniki span.b-share-counter').html(odnCount + 4);
$('.b-share-btn__moimir span.b-share-counter').html(moimirCount + 3);
$('.b-share-btn__gplus span.b-share-counter').html(gglCount + 35);
}

// аб тест
$(function() {
var random = Math.random();

if(random >= '0.5') {var rand = 1;} else {var rand = 2;};

if (getCookie('ab_test')==null) {
if(rand == 1) {
var yaParams = {likes: "variant_1"};
setCookie('ab_test', '1', 1000*60*60*30);
} else if(rand == 2) {
var yaParams = {likes: "variant_2"};
setCookie('ab_test', '2', 1000*60*60*30);
setTimeout(likeUp, 5000);
}
} else if(getCookie('ab_test') == '1') {
var yaParams = {likes: "variant_1"};
} else if(getCookie('ab_test') == '2') {
var yaParams = {likes: "variant_2"};
setTimeout(likeUp, 5000);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question