Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed problem with customized StorageAdapter. The specified value was…

… never stored because this.storageAdapter was used instead of this.options.storageAdapter.

Also removed the this.trackEvent first parameter '_trackEvent' to trackEvent function for the GoogleAnalyticsAdapter (trackEvent have that already specified)
  • Loading branch information...
commit db951dba7b7e82060c4e1391ca7a68bfd4ad8074 1 parent cc2c9a6
@jorkas jorkas authored committed
Showing with 82 additions and 5 deletions.
  1. +5 −5 lib/cohorts.js
  2. +77 −0 test/test_storageAdapter.html
View
10 lib/cohorts.js
@@ -20,7 +20,7 @@ Cohorts = (function() {
}
},
onEvent: function(testName, cohort, eventName) {
- this.trackEvent('_trackEvent', this.nameSpace, testName, cohort + ' | ' + eventName);
+ this.trackEvent(this.nameSpace, testName, cohort + ' | ' + eventName);
}
};
@@ -44,8 +44,8 @@ Cohorts = (function() {
throw('Cohorts must be specified for this test');
if(Utils.size(options.cohorts) < 2)
throw('You must specify at least 2 cohorts for a test');
- if(!this.storageAdapter)
- this.storageAdapter = GoogleAnalyticsAdapter;
+ if(!this.options.storageAdapter)
+ this.options.storageAdapter = GoogleAnalyticsAdapter;
this.cohorts = Utils.keys(this.options.cohorts);
@@ -86,7 +86,7 @@ Cohorts = (function() {
} else {
var chosen_cohort = this.getCohort();
}
- this.storageAdapter.onInitialize(in_test, this.options.name, chosen_cohort);
+ this.options.storageAdapter.onInitialize(in_test, this.options.name, chosen_cohort);
// call the onChosen handler, if it exists
if(this.options.cohorts[chosen_cohort].onChosen)
@@ -97,7 +97,7 @@ Cohorts = (function() {
},
event: function(eventName) {
if(this.inTest())
- this.storageAdapter.onEvent(this.options.name, this.getCohort(), eventName);
+ this.options.storageAdapter.onEvent(this.options.name, this.getCohort(), eventName);
},
inTest: function() {
if(this.getCookie('in_test') == 1) {
View
77 test/test_storageAdapter.html
@@ -0,0 +1,77 @@
+<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: {},
+ },
+ storageAdapter: {
+ nameSpace: 'storageAdapter_cohorts',
+ trackEvent: function(category, action, opt_label, opt_value) {
+ if(window['_gaq']) {
+ _gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
+ } else {
+ 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) {
+ 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>
Please sign in to comment.
Something went wrong with that request. Please try again.