Skip to content

Instantly share code, notes, and snippets.

@GoingOffRoading
Created February 3, 2014 19:39
Show Gist options
  • Save GoingOffRoading/8790840 to your computer and use it in GitHub Desktop.
Save GoingOffRoading/8790840 to your computer and use it in GitHub Desktop.
CatPageJS
(function($){
jQuery.getScript('//media.richrelevance.com/rrserver/js/1.0/p13n.js', function(){
//session cookie functions
function C(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]}
function guidGenerator() {
var S4 = function() {
return ((((1+Math.random())*0x10000)|0)).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function guidCheck(){
if(C('guId') == null){
var guId = guidGenerator();
document.cookie= 'guId='+guId+'; path=/';
}
return C('guId');
}
//end session cookie
//api key check
var api = '808eb9bd4473db38',
url = window.location.host.split('.').splice(1,2).join('.');
switch(url){
case 'ventingdirect.com':
api = '673e8a2cc8112d6b';
break;
case 'handlesets.com':
api = '034dfc5b29a514b5';
break;
case 'ventingpipe.com':
api = '5248157165abff52';
break;
case 'pullsdirect.com':
api = 'ba7a2cdcee51c57b';
break;
case 'faucetdirect.com':
api = '89e8a23040a889f3';
break;
case 'lightingdirect.com':
api = 'fd40a20d8a0d6713';
break;
}
var env = 'recs';
if(dataLayer.environment !== 'production'){
env = 'integration';
}
//rich relevance stuff
RR.jsonCallback = function(){
/*shows the recs when they are completely loaded - Chase 1/6/14 */
$(window).trigger('recommendations.loaded');
$.each(RR.data.JSON.placements, function(index, item){
var order = index + 1,
heading = item.strat_message,
items = item.items;
if($('#rr_'+order).length < 1){
var content = '<div id="rr_'+order+'" class="modulebox rr-recs-beta rrBox clearfix">'+
'<div class="rr_boxwrapper">'+
'<div class="rr_boxhead">'+
'<h2 class="heading">'+heading+'</h2>'+
'</div>'+
'<div class="rr_boxbody rrContainer clearfix">'+
'<div class="recs-slider-buttons">'+
'<div class="recs-scroller-head">'+
'<a class="featured-previous left" href="javascript:void(0);"></a>'+
'<a class="featured-next right" href="javascript:void(0);"></a>'+
'</div>'+
'<div style="clear: both;"></div>'+
'</div>'+
'<div class="recs-stage jcarousel clearfix">'+
'<ul class="featured_list clearfix">'+
'</ul>'+
'</div>'+
'</div>'+
'</div>'+
'</div>';
//Category Page $('#main').append(content);
//Product Drop $('#searchResults').append(content);}
//Product Search $('#searchResults').append(content);}
if (dataLayer.page == 'browse:category'){
$('#main').prepend(content);}
else if (dataLayer.page === 'search:browse' && dataLayer.id != '0'){
$('#main').append(content);}
else {$('#main').prepend(content);
}
}
$('#rr_'+order).find('h3').text(heading);
var id = '';
if(dataLayer.id != '' && dataLayer.id != 0 && dataLayer.id != undefined){
id = ':'+dataLayer.id;
}
$.each(items, function(index, item){
var rrBrand = item.brand,
rrId = item.id,
rrImgUrl = item.imageURL,
rrIndex = item.index,
rrLinkUrl = item.linkURL,
rrName = item.name,
rrIntCmp = 'recs_page:'+dataLayer.page+id+'_recBox:categoryPage',
rrPrice = (parseFloat(item.price.replace(',', '')) == parseFloat(item.price_high)) ? '$'+parseFloat(item.price.replace(',','')).toFixed(2) : ('$'+parseFloat(item.price.replace(',','')).toFixed(2)+' - $'+parseFloat(item.price_high).toFixed(2)),
rrRating = item.rating,
rrReviews = item.reviews,
rrTitle = (item.title.length <= 75) ? item.title : (item.title.slice(0,75) + '&hellip;');
var rrItem = '<li class="rr_product">'+
'<div class="rr_product_image_link">'+
'<a rel="nofollow" data-intcmp="'+rrIntCmp+'" href="'+rrLinkUrl+'" id="recs-product-image-link">'+
'<img src="'+rrImgUrl+'" alt="'+rrName+'" width="150" class="recs-product-image">'+
'</a>'+
'</div>'+
'<div class="rr_product_name">'+
'<a rel="nofollow" data-intcmp="'+rrIntCmp+'" href="'+rrLinkUrl+'">'+rrName+'</a>'+
'</div>'+
'<a rel="nofollow" data-intcmp="'+rrIntCmp+'"href="'+rrLinkUrl+'" class="rrTitle-link"><span class="rrName">'+rrTitle+'</span></a>';
rrItem += '<div class="rr_list">'+
'<span class="rr_list_price">'+rrPrice+'</span>'+
'</div>';
if(typeof rrRating != 'undefined'){
rrItem += '<div class="reviewRating">'+
'<div class="rating r'+(Math.round(rrRating / 0.5) * 0.5).toString().replace('.','')+'" title="User Rating: '+rrRating+' out of 5">'+
'<div></div>'+
'</div>'+
'<meta content="'+rrRating+'">';
if(rrReviews > 0){
rrItem += '<span class="reviewCount">('+rrReviews+')</span></div>';
}
}
rrItem += '</li>';
$('#rr_'+order).find('.featured_list').append(rrItem);
});
});
$('.rr-recs-beta a[data-intcmp]').on('click', function(e){
e.preventDefault();
var $this = $(this);
var s = window.s_gi(window.s_account);
s.linkTrackVars = "eVar2";
s.eVar2 = $this.data('intcmp');
s.tl(this, 'o', 'Internal Campaign');
setTimeout(function(){
window.location = $this.attr('href');
}, 300);
});
var jCarouselUrl = '/themes/build/scripts/vendor/jquery/jquery.jcarousel.js';
jQuery.getScript(jCarouselUrl, function() {
jQuery('.rrBox.rr-recs-beta').each(function() {
var recs = jQuery(this),
c = recs.find('.jcarousel'),
prev = recs.find('.featured-previous'),
next = recs.find('.featured-next'),
pages = jQuery('<div class="recs-pages"/>'),
count = c.data('size') || 4,
maxPages = 0;
c.on('scrollend.jcarousel', function(e, carousel) {
var t = c.jcarousel('target'),
pos = t.index(),
page = Math.min(Math.ceil(pos/count), maxPages-1);
pages.find('span').removeClass('active').eq(page).addClass('active');
}).on('create.jcarousel', function(e, carousel) {
var total = c.jcarousel('items').length,
i = 0;
maxPages = Math.ceil(total / count);
c.append(pages);
for(i = 0; i < maxPages; i++) {
var span = jQuery('<span />').click(function() {
c.jcarousel('scroll', jQuery(this).index()*count, true);
});
pages.append(span);
}
pages.find('span').eq(0).addClass('active');
}).jcarousel({
scroll: count,
wrap: 'both'
});
prev.click(function(e) {
e.preventDefault();
c.jcarousel('scroll', "-="+count, true);
});
next.click(function(e) {
e.preventDefault();
c.jcarousel('scroll', "+="+count, true);
});
});
});
};
window.R3_COMMON = new r3_common();
R3_COMMON.setApiKey(api);
R3_COMMON.setBaseUrl(window.location.protocol+'//'+env+'.richrelevance.com/rrserver/');
R3_COMMON.setClickthruServer(window.location.protocol+"//"+window.location.host);
R3_COMMON.setSessionId(guidCheck());
if(dataLayer.emailId){
R3_COMMON.setUserId(dataLayer.emailId);
}else if(dataLayer.userId){
R3_COMMON.setUserId(dataLayer.userId);
}else{
R3_COMMON.setUserId(C('CID'));
}
//request recommendations for page area (1 per call)
//browse:category is category pages
//browse:search id not 0 is product drop
//browse:search id = 0 is SOLR search
if (dataLayer.page == 'browse:category'){
R3_COMMON.addPlacementType('category_page.rr1');
} else if (dataLayer.page === 'search:browse' && dataLayer.id != '0'){
R3_COMMON.addPlacementType('category_page.rr2');
} else if (dataLayer.page === 'search:browse' && dataLayer.id == '0'){
R3_COMMON.addPlacementType('category_page.rr1');
console.log('true');
}
//Here we're setting category page results for cat pages
if(dataLayer.id != 0){
window.R3_CATEGORY = new r3_category();
R3_CATEGORY.setId(dataLayer.id.toString());
// dataLayer.name is giving us the document.title.
// we need to grab the last category name from the
// breadcrumbs
var breadcrumbList = dataLayer.breadcrumbs.split('|');
var catName = breadcrumbList[breadcrumbList.length -1];
R3_CATEGORY.setName(catName);
}
//else will be search data
else if (dataLayer.id ==0){
var R3_SEARCH = new r3_search();
R3_SEARCH.setTerms('dataLayer.searchTerm');
// for each item (LIMIT of 15) on the search page, iterate over the list with the following setter:
R3_SEARCH.addItemId('[id]'); // if item has parent id, pass the parent id
}
r3();
rr_flush_onload();
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment