简体   繁体   English

使用javascript或jquery跟踪AB点击

[英]track AB clicks with javascript or jquery

I want to run build some simple AB testing on a website. 我想在网站上运行一些简单的AB测试。 I would like it to be as light as possible to avoid slowing down pageload, and plan to run a single test at a time, with a single variable. 我希望它尽可能轻巧,以免减慢页面加载速度,并计划一次使用单个变量运行一次测试。 I'm not skilled with javascript, but with some hammering away, I got a simple 50/50 randomizer running: 我不精通javascript,但是经过一番锤炼,我运行了一个简单的50/50随机发生器:

var randomNumber=Math.floor(Math.random()*2)+1;{
  if (randomNumber == 1)
    document.getElementById("id1").innerHTML = "Please Signup";

  if (randomNumber == 2)
    document.getElementById("id1").innerHTML = "I insist you signup!";
}

http://www.bootply.com/0oum4mlhzW http://www.bootply.com/0oum4mlhzW

I am lost at the next step though. 但是,我迷失了下一步。 I want to track the clicks on signup button, based on what content was showing at the time. 我想根据当时显示的内容来跟踪注册按钮的点击次数。 I'll need to track # of visitors, # of clicks, and then be able to see this content in html somewhere. 我需要跟踪#个访问者,#个点击,然后才能在html某处看到此内容。

Would also like to use cookies (or another method) to make sure a returning visitor always sees the same content. 还希望使用Cookie(或其他方法)来确保回访者始终看到相同的内容。

Do you mean something like this? 你的意思是这样吗?

JSFiddle JSFiddle

var btn = document.getElementById('target1');
var track = document.getElementById('track');

// Retrieve
if (localStorage.getItem("trackNum") != ""){
     track.value=localStorage.getItem("trackNum");
}

var cnt = track.value;
btn.onclick = function(){
    cnt++;
    track.value = cnt;

    // Store
    localStorage.setItem("trackNum", track.value);
};

HTML: HTML:

<div class="container">
  <h1 id="id1">Do you think you might consider signing up?</h1>
  <button id="target1" class="btn btn-primary">Sign up!</button>
    <input type="text" id="track" value="0"/>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM