From 16ee253b62c3f88e021e22a5e1b02e298263c2e5 Mon Sep 17 00:00:00 2001 From: Alessandro Coscia Date: Wed, 31 Oct 2012 19:06:55 +0100 Subject: [PATCH] Cohort analysis on more that one page The prevous Cohort framework could do tests only in one page because the onInitialize event was called every time, even if a user was in a certain Cohort in the previous page. Now onInitialize has the parameter "new_to_this_test" so that you can call the event "cohort + ' | Total'" only when the user enters to the test, and not all the times the test is initialized. --- lib/cohorts.js | 13 ++++-- test/dev_storageAdapter.html | 82 +++++++++++++++++++++++++++++++++++ test/test_storageAdapter.html | 4 +- 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100755 test/dev_storageAdapter.html diff --git a/lib/cohorts.js b/lib/cohorts.js index 5318adf..d6317ef 100644 --- a/lib/cohorts.js +++ b/lib/cohorts.js @@ -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'); } }, @@ -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); @@ -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) diff --git a/test/dev_storageAdapter.html b/test/dev_storageAdapter.html new file mode 100755 index 0000000..615a42f --- /dev/null +++ b/test/dev_storageAdapter.html @@ -0,0 +1,82 @@ + + + + Cohorts Test with StorageAdapter + + + + + +

+ This is a big header +

+ +

+ This is a small header +

+ + + + + \ No newline at end of file diff --git a/test/test_storageAdapter.html b/test/test_storageAdapter.html index 5d39165..48f9a30 100644 --- a/test/test_storageAdapter.html +++ b/test/test_storageAdapter.html @@ -53,8 +53,8 @@

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'); } },