Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/cohorts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Cohorts = (function() {
throw(" _gaq object not found: It looks like you haven't correctly setup the asynchronous Google Analytics tracking code, and you are using the default GoogleAnalyticsAdapter.");
}
},
onInitialize: function(inTest, testName, cohort) {
if(inTest) {
onInitialize: function(inTest, testName, cohort, new_to_this_test) {
if(inTest && new_to_this_test) {
this.trackEvent(this.nameSpace, testName, cohort + ' | Total');
}
},
Expand Down Expand Up @@ -71,8 +71,13 @@ Cohorts = (function() {

// Determine whether user should be in the test
var in_test = this.inTest();
if(in_test === null) // haven't seen this user before
var new_to_this_test = null;
if(in_test === null) { // haven't seen this user before
in_test = Math.random() <= this.options.sample;
if (in_test) new_to_this_test = true;
}
else
new_to_this_test = false;

if(in_test) {
this.setCookie('in_test', 1);
Expand All @@ -86,7 +91,7 @@ Cohorts = (function() {
} else {
var chosen_cohort = this.getCohort();
}
this.options.storageAdapter.onInitialize(in_test, this.options.name, chosen_cohort);
this.options.storageAdapter.onInitialize(in_test, this.options.name, chosen_cohort, new_to_this_test);

// call the onChosen handler, if it exists
if(this.options.cohorts[chosen_cohort].onChosen)
Expand Down
82 changes: 82 additions & 0 deletions test/dev_storageAdapter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Cohorts Test with StorageAdapter</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../lib/cohorts.js"></script>
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16758721-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
</head>
<body id="test" onload="">
<h1 style="display:none" id="big">
<a href="#">This is a big header</a>
</h1>

<h2 id="small">
<a href="#">This is a small header</a>
</h2>


<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
Cohorts.Options.debug = true;

header_test = new Cohorts.Test({
name: 'storageAdapter_big_vs_small_header',
sample: 1,
cohorts: {
big: {
onChosen: function() {
$('#big').show();
$('#small').hide();
}
},
small: {},
},
// This storage adapter just push events in browser console
// and gives you a warning if Google Analytics is not included
// It is designed for development environments
storageAdapter: {
nameSpace: 'cohorts',
trackEvent: function(category, action, opt_label, opt_value) {
console.log('trackEvent: ' + category + ', ' + action + ', ' + opt_label + ', ' + opt_value);

if(window['_gaq']) {
// this does nothing because this is a test adapter
} else {
console.log("_gaq object not found: It looks like you haven't correctly setup the asynchronous Google Analytics tracking code, and you are using the default GoogleAnalyticsAdapter.");
}
},
onInitialize: function(inTest, testName, cohort, new_to_this_test) {
if(inTest && new_to_this_test) {
this.trackEvent(this.nameSpace, testName, cohort + ' | Total');
}
},
onEvent: function(testName, cohort, eventName) {
this.trackEvent(this.nameSpace, testName, cohort + ' | ' + eventName);
}
}
});

$('#big').click(function() {
header_test.event('Clicked on Big Header');
});

$('#small').click(function() {
header_test.event('Clicked on Small Header');
});
});
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions test/test_storageAdapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ <h2 id="small">
throw(" _gaq object not found: It looks like you haven't correctly setup the asynchronous Google Analytics tracking code, and you are using the default GoogleAnalyticsAdapter.");
}
},
onInitialize: function(inTest, testName, cohort) {
if(inTest) {
onInitialize: function(inTest, testName, cohort, new_to_this_test) {
if(inTest && new_to_this_test) {
this.trackEvent(this.nameSpace, testName, cohort + ' | Total');
}
},
Expand Down