Changeset 1460474
- Timestamp:
- 07/25/2016 10:07:33 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
simple-content-experiments/trunk/simple-content-experiments.js
r1460390 r1460474 13 13 value: 0 14 14 }, 15 ex_list: {}, // list of experiments (as marked up in DOM) 15 16 dbgmsg: function(str){ 16 17 if (simple_content_experiements_debug){ console.log(str);} // uses global simple_content_experiements_debug … … 32 33 // check for "Universal" Analytics (ga) 33 34 if ( "unknown" == retval){ 34 if ( "string" == typeof ( window["GoogleAnalyticsObject"])){35 content_experiment.ga_name = window["GoogleAnalyticsObject"] .35 if ( "string" == typeof window["GoogleAnalyticsObject"]){ 36 content_experiment.ga_name = window["GoogleAnalyticsObject"]; 36 37 retval = "universal"; 37 38 } … … 69 70 }, 70 71 analytics_event: function(){ 71 content_experiment.dbgmsg("analytics type: " + content_experiment.event_flavor);72 content_experiment.dbgmsg("analytics_event: analytics type: " + content_experiment.event_flavor); 72 73 73 74 // sometimes the scripts load in the wrong order and Google Analytics won\'t get an event from chooseVariation() … … 108 109 break; 109 110 default: 110 content_experiment.dbgmsg("event_flavor: " + content_experiment.event_flavor);111 content_experiment.dbgmsg("event_flavor: " + content_experiment.event_flavor); 111 112 } 112 113 114 115 116 } 113 114 }, 115 run_experiment: function(){ 116 content_experiment.dbgmsg("run_experiment"); 117 // create list of experiments (as marked up in DOM) and how many variations: 118 jQuery( ".contentexperimentvariant" ).each( 119 function( ) { 120 var v_id = jQuery( this ).attr("data-contentexperimentid"); 121 if( "undefined" == typeof content_experiment.ex_list[v_id] ) { 122 content_experiment.ex_list[v_id] = 0; 123 } 124 content_experiment.ex_list[v_id]++; 125 } 126 ); 127 // content_experiment.ex_list is object of experiment ID with counts 128 var keys = Object.keys(content_experiment.ex_list); 129 if (keys.length > 0){ // no experiments 130 content_experiment.dbgmsg("detected " + keys.length + " experiment(s)."); 131 // pick an experiment to run on this page: 132 var i = Math.floor((Math.random() * keys.length)); // 0 based 133 content_experiment.id = keys[i]; 134 content_experiment.variant_count = content_experiment.ex_list[content_experiment.id]; 135 jQuery.getScript( 136 "//www.google-analytics.com/cx/api.js?experiment=" + content_experiment.id, 137 function(){ // callback function after lib is loaded: 138 // Note that the library has been loaded: 139 content_experiment.cx_loaded = "loaded_and_called"; 140 141 // Ask Google Analytics which variation to show the user: 142 content_experiment.selected = cxApi.chooseVariation(); // 0 based options 143 // Add some extra data for event tracking: 144 for ( i = 1; i <= content_experiment.variant_count; i++) { 145 jQuery("#" + content_experiment.id + "__" + i).find("a").attr("data-xvariant", i); 146 jQuery("#" + content_experiment.id + "__" + i).find("a").addClass("xvariantclick"); 147 } 148 content_experiment.display_experiment(); 149 content_experiment.analytics_event(); // move this until after we're sure it's loaded? 150 } 151 ) 152 .done(function( script, textStatus ) { 153 if ( "pending" == content_experiment.cx_loaded ){ 154 content_experiment.cx_loaded = "loaded"; 155 } 156 }) 157 .fail(function( jqxhr, settings, exception ) { 158 console.log( "getScript: Triggered ajaxError handler." ); 159 content_experiment.action = "Fail"; 160 content_experiment.label = "jQuery.getScript"; 161 content_experiment.cx_loaded = false; 162 }) 163 164 } else { 165 content_experiment.dbgmsg("no experiments detected."); 166 } 167 } 117 168 118 169 }; … … 129 180 */ 130 181 jQuery(document).ready(function() { 131 console.log('help me!'); 132 var ex_list = {}; 182 133 183 var flavor = content_experiment.get_analytics_event_flavor(); 134 content_experiment.dbgmsg("flavor: " + flavor);184 content_experiment.dbgmsg("flavor: " + flavor); 135 185 content_experiment.event_flavor = flavor; 136 186 if ('gtm' != flavor){ 187 content_experiment.run_experiment(); 188 } else { 189 content_experiment.dbgmsg("Defering experiment. Let GTM call back."); 190 } 137 191 138 139 jQuery( ".contentexperimentvariant" ).each(140 function( ) {141 var v_id = jQuery( this ).attr("data-contentexperimentid");142 if(typeof ex_list[v_id] == "undefined") {143 ex_list[v_id] = 0;144 }145 ex_list[v_id]++;146 }147 );148 // ex_list is object of experiment ID with counts149 var keys = Object.keys(ex_list);150 if (keys.length > 0){151 // pick an experiment to run on this page:152 var i = Math.floor((Math.random() * keys.length)); // 0 based153 content_experiment.id = keys[i];154 content_experiment.variant_count = ex_list[content_experiment.id];155 content_experiment.dbgmsg(content_experiment.id);156 jQuery.getScript(157 "//www.google-analytics.com/cx/api.js?experiment=" + content_experiment.id,158 function(){ // callback function after lib is loaded:159 // Note that the library has been loaded:160 content_experiment.cx_loaded = "loaded_and_called";161 162 // Ask Google Analytics which variation to show the user:163 content_experiment.selected = cxApi.chooseVariation(); // 0 based options164 // Add some extra data for event tracking:165 for ( i = 1; i <= content_experiment.variant_count; i++) {166 jQuery("#" + content_experiment.id + "__" + i).find("a").attr("data-xvariant", i);167 jQuery("#" + content_experiment.id + "__" + i).find("a").addClass("xvariantclick");168 }169 content_experiment.display_experiment();170 content_experiment.analytics_event(); // move this until after we're sure it's loaded?171 }172 )173 .done(function( script, textStatus ) {174 if ( "pending" == content_experiment.cx_loaded ){175 content_experiment.cx_loaded = "loaded";176 }177 })178 .fail(function( jqxhr, settings, exception ) {179 console.log( "getScript: Triggered ajaxError handler." );180 content_experiment.action = "Fail";181 content_experiment.label = "jQuery.getScript";182 content_experiment.cx_loaded = false;183 })184 185 }186 192 });
Note: See TracChangeset
for help on using the changeset viewer.