Created
October 6, 2015 16:09
-
-
Save doublejosh/5a9f2c4ca3c4f5b45709 to your computer and use it in GitHub Desktop.
Backup of recommender switcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Swap recommender blocks for effectiveness testing. | |
| */ | |
| (function ($) { | |
| $(document).ready(function() { | |
| // Show at all? Consistent by user. (TEMPORARY)... | |
| var should_show = $.cookie('recommend'); | |
| // Get value, allow employee override. | |
| if ($('body.internal').length > 0) { | |
| should_show = 1; | |
| } | |
| // If this user is a yes OR no choice has been made an random is yes. | |
| if (should_show == 1 || (should_show == null && Math.random() < 0.2)) { | |
| // Set cookie to yes. | |
| $.cookie('recommend', 1, { path : '/' }); | |
| // Allow picking amoung several blocks. | |
| if (Drupal.settings.tableau_recommended.block_picker == 1) { | |
| // Define blocks. | |
| var $b1 = $('#block-views-recommended-similar-block-6'); | |
| var $b2 = $('#block-views-recommended-similar-block'); | |
| // Chose one. | |
| if ($b1.length && $b2.length) { | |
| // Avoiding faux dynamic variable names in JS. | |
| switch (Math.random() < 0.5) { | |
| case true: | |
| var $unchosen = $b1; | |
| var $chosen = $b2; | |
| break; | |
| case false: | |
| var $unchosen = $b2; | |
| var $chosen = $b1; | |
| break; | |
| } | |
| $unchosen.hide(); | |
| } | |
| } | |
| else { | |
| // No choosing. | |
| } | |
| } | |
| else { | |
| // Most of the time just hide it all. | |
| $.cookie('recommend', 0, { path : '/' }); | |
| // Hide it. | |
| $('#zone-content-below-wrapper').hide(); | |
| } | |
| }); | |
| /** | |
| * Add analytics to links within recommendations. | |
| * NOTE: Able to happen on AJAX returns. | |
| */ | |
| Drupal.behaviors.tableau_recommended_analysis = { | |
| attach: function (context, settings) { | |
| // Prevent firing on Views context. | |
| if (context == document || (context instanceof jQuery && context.is('form'))) { | |
| var $blocks = $('#region-content-below [class*="block-views-recommended-similar-block"]:visible'); | |
| $blocks.each(function() { | |
| var $el = $(this); | |
| $el.find('.content li a').each( function() { | |
| // Add query params if not already present (AJAX can cause duplication). | |
| if (typeof $(this).attr('href').split('src=recommended')[1] === 'undefined') { | |
| var glue = '?'; | |
| if (typeof $(this).attr('href').split('?')[1] !== 'undefined') { | |
| glue = '&'; | |
| } | |
| $(this).attr('href', | |
| $(this).attr('href') + glue + 'src=recommended&block=' + $el.attr('id') | |
| ); | |
| } | |
| }); | |
| }); | |
| } | |
| } | |
| }; | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment