var Incito = {}; // base configuration Incito = { catalogConfig: { order_by: '-valid_date', types: 'paged,incito', offset: 0, limit: 6 }, config: { dealer_id: '', openIncito: SGN.util.getQueryParam('openIncito'), openCategory: SGN.util.getQueryParam('openCategory'), dataId: '', targetId: '', maxRetry: 3, curRetry: 0, locale: 'da-DK', weeklyLength: 8, testLogging: false, stagingServer: false }, publication: { activePublication: {} }, data: { targetElement: {}, bootStrapper: {}, viewer: {} }, regex: { weekly: { 'da-DK': /^Uge \d{1,2}/, 'nb-NO': /^Uke \d{1,2}/, 'sv-SE': /^Vecka \d{1,2}/, 'fi-FI': /^Viikko \d{1,2}/ }, b2b: { 'da-DK': /^Erhverv .*/, 'nb-NO': /^B2B .*/, 'sv-SE': /^B2B .*/, 'fi-FI': /^B2B .*/ } }, search: { searchParam: '', searchResult: [], relatedResult: [], searchWording: { 'da-DK': 'Henter resultater ', 'nb-NO': 'Searching ', 'sv-SE': 'Searching ', 'fi-FI': 'Searching ' }, resultText: { 'da-DK': [' Resultater fundet', ' Relaterede produkter fundet'], 'nb-NO': [' Resultater fundet', ' Relaterede produkter fundet'], 'sv-SE': [' Resultater fundet', ' Relaterede produkter fundet'], 'fi-FI': [' Resultater fundet', ' Relaterede produkter fundet'] } }, dateFormat: { 'da-DK': ['Fra ', '/', ' t.o.m. ', '/'], 'nb-NO': ['Fra ', '/', ' til ', '/', ' 08:00'], 'sv-SE': ['Från ', '/', ' t.o.m. ', '/'], 'fi-FI': ['', '/', ' - ', '/'] } }; //google analytics var noop = function () {}; var nga = 'dataLayer' in window ? function (ctx) { dataLayer.push({ 'event': 'shopgun', 'shopgunCategory': ctx.eventCategory, 'shopgunAction': ctx.eventAction, 'shopgunLabel': ctx.eventLabel }); } : noop; var once = function (fun) { var done = false; return function () { if (!done) { done = true; return fun.apply(this, arguments); } }; }; var getPublicationRuntimeEventLabel = function (data) { //console.log(data.label + ' - ' + data.run_from.substr(0, 10)); return data.label + ' - ' + data.run_from.substr(0, 10); //return data.run_from.substr(0, 10) + '/' + data.run_till.substr(0, 10); }; var updateQueryStringParameter = function (uri, key, value) { var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); var separator = uri.indexOf('?') !== -1 ? "&" : "?"; if (uri.match(re)) { return uri.replace(re, '$1' + key + "=" + value + '$2'); } else { return uri + separator + key + "=" + value; } }; // used to add configuration settings to the script before running. Incito.Configure = function(configuration){ $.extend( Incito.config, configuration ); if(Incito.config.stagingServer == true){ SGN.config.set({ coreUrl: 'https://squid-api.tjek-staging.com', graphUrl: 'https://squid-api.tjek-staging.com' }); } } // loads publication on the offerspage based on openIncito variable in the adress. // current / future are speciels calls to get those regardless of ID. Incito.LoadPublication = function(){ Incito.TestLogging(SGN.config); if(Incito.config.openIncito !== undefined && Incito.config.dataId === ''){ if(Incito.config.openIncito === 'current' || Incito.config.openIncito === 'future'){ var configuration = { dealer_id: Incito.config.dealer_id }; $.extend( configuration, Incito.catalogConfig ); var r = []; Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); if (!err && res && res.length > 0) { res.reverse(); current = new Date(); future = new Date(); future.setDate(current.getDate()+Incito.config.weeklyLength); for(i = 0; i < res.length; i++){ if(Incito.regex.weekly[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'current'){ Incito.TestLogging(res[i].id); Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } else if(Incito.ToDate(res[i].run_from) < future && Incito.ToDate(res[i].run_till) > future && Incito.config.openIncito === 'future'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } if(Incito.config.openIncito === 'future' && Incito.config.dataId === ''){ Incito.config.openIncito = 'current'; for(i = 0; i < res.length; i++){ if(Incito.regex.weekly[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'current'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } } Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } }); } else if (Incito.config.openIncito === 'currentB2B' || Incito.config.openIncito === 'futureB2B'){ var configuration = { dealer_id: Incito.config.dealer_id }; $.extend( configuration, Incito.catalogConfig ); var r = []; Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); if (!err && res && res.length > 0) { current = new Date(); future = new Date(); future.setDate(current.getDate()+Incito.config.weeklyLength); for(i = 0; i < res.length; i++){ if(Incito.regex.b2b[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'currentB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } else if(Incito.ToDate(res[i].run_from) < future && Incito.ToDate(res[i].run_till) > future && Incito.config.openIncito === 'futureB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } if(Incito.config.openIncito === 'futureB2B' && Incito.config.dataId === ''){ Incito.config.openIncito = 'currentB2B'; for(i = 0; i < res.length; i++){ if(Incito.regex.b2b[Incito.config.locale].test(res[i].label)){ if(Incito.ToDate(res[i].run_till) > current && Incito.ToDate(res[i].run_till) < future && Incito.config.openIncito === 'futureB2B'){ Incito.config.dataId = res[i].id; incitoDataInput.Heading.Title = res[i].label; } } } } Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } }); } else { Incito.config.dataId = Incito.config.openIncito; Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } } else if(Incito.config.dataId !== ''){ Incito.GetPubliction(); loadingPublication = setInterval(Incito.LoadingPublication, 1000); } else{ Incito.TestLogging('No publication selected'); } }; // Tries to get the publication data from the incito API. Incito.GetPubliction = function(){ Incito.TestLogging('GetPubliction: ' + Incito.config.dataId); Incito.TestLogging('TargetId: ' + Incito.config.targetId); Incito.data.targetElement = document.getElementById(Incito.config.targetId); Incito.data.bootStrapper = new SGN.IncitoPublicationKit.Bootstrapper({ el: Incito.data.targetElement, id: Incito.config.dataId, eventTracker: SGN.config.get('eventTracker') }); Incito.data.bootStrapper.fetch(function (err, res) { Incito.TestLogging(err); Incito.TestLogging(res); Incito.publication.activePublication = res; Incito.publication.LoadingCompleted = true; }); }; // loop to make sure that publication loads correctly. Incito.LoadingPublication = function(){ if(Incito.publication.LoadingCompleted){ if(Incito.publication.activePublication == {} && Incito.config.curRetry < Incito.config.maxRetries){ Incito.TestLogging('retrying to get Incito'); Incito.publication.activePublication = undefined; Incito.config.curRetry++; } else if(Incito.publication.activePublication == {} && Incito.config.curRetry >= Incito.config.maxRetry){ console.error('max retries reached, stopping'); clearInterval(loadingPublication); } else if(Incito.publication.activePublication !== {}){ clearInterval(loadingPublication); Incito.OpenPublication(); } } }; // opens the loaded publication into the target element set in configuration. Incito.OpenPublication = function(){ //Incito.TestLogging(new Date()); //Incito.TestLogging(Incito.publication.activePublication.details); Incito.data.viewer = Incito.data.bootStrapper.createViewer({ incito: Incito.publication.activePublication.incito, details: Incito.publication.activePublication.details }); Incito.data.viewer.start(); if(Incito.publication.activePublication.details.label !== undefined && Incito.publication.activePublication.details.label !== ''){ incitoDataInput.Heading.Title = Incito.publication.activePublication.details.label; nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Opened', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Opened', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); } Incito.data.viewer.incito.bind('allRendered', function () { //Incito.TestLogging('allRendered'); OffersPage.Activate(); //Incito.TestLogging(new Date()); }); Incito.AddScrollElement(); Incito.AddHotspotLinks(); }; // adds links to the products. Incito.AddHotspotLinks = function(){ SGN.CoreUIKit.on(Incito.data.targetElement, 'click', '.incito__view[data-role="offer"]', function (e) { e.preventDefault(); var id = this.getAttribute('data-id'); var meta = Incito.data.viewer.incito.ids[id]; if (!meta || !meta['tjek.offer.v1'] || !meta['tjek.offer.v1'].link) { return; } //var url = updateQueryStringParameter(meta['tjek.offer.v1'].link, 'intcid', 'INT_INCITO_BUTTON'); var url = updateQueryStringParameter(meta['tjek.offer.v1'].link, 'itm_source', 'incito'); var url = updateQueryStringParameter(url, 'itm_campaign', incitoDataInput.Heading['Title'].replace(' ', '_') + '_' + new Date().getFullYear()); var url = updateQueryStringParameter(url, 'itm_medium', 'incito_product_link'); var url = updateQueryStringParameter(url, 'itm_content', meta['tjek.offer.v1'].ids[0].value); nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Offer Opened', 'eventLabel': id }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Offer Opened', id); window.open(url, '_blank'); }); }; // activate the scroll percentage counter. Incito.AddScrollElement = function(){ var trackProgress = function (progress) { nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Read-through ' + progress + '%', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Read-through ' + progress + '%', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); }; var navigationHandlers = { page2: once(function () { nga({ 'eventCategory': 'Incito Publication', 'eventAction': 'Read-through page 2', 'eventLabel': getPublicationRuntimeEventLabel(Incito.publication.activePublication.details) }); //ga('IncitoTracker.send', 'event', 'Incito Publication', 'Read-through page 2', getPublicationRuntimeEventLabel(Incito.publication.activePublication.details)); }), progress20: once(function () { trackProgress(20) }), progress40: once(function () { trackProgress(40) }), progress60: once(function () { trackProgress(60) }), progress80: once(function () { trackProgress(80) }), progress100: once(function () { trackProgress(100) }) }; Incito.data.viewer.bind('progress', function (navEvent) { if (navEvent.progress >= 100) { navigationHandlers.progress100(); } else if (navEvent.progress >= 80) { navigationHandlers.progress80(); } else if (navEvent.progress >= 60) { navigationHandlers.progress60(); } else if (navEvent.progress >= 40) { navigationHandlers.progress40(); } else if (navEvent.progress >= 20) { navigationHandlers.progress20(); } }); }; Incito.RenderCatalogs = function(configurationInput){ configuration = {}; $.extend( configuration, Incito.catalogConfig ); $.extend( configuration, configurationInput ); if(configuration.locale !== undefined){ Incito.config.locale = configuration.locale; } if(configuration.catalogList !== undefined && configuration.catalogList.length > 0){ Incito.RenderSelectedCatalogs(configuration); } else { Incito.RenderPublishedCatalogs(configuration); } }; Incito.RenderPublishedCatalogs = function(configuration) { Incito.FetchCatalogs(configuration, function (err, res) { Incito.TestLogging(res); if (err){console.error(err)} else if(!err && res && res.length > 0){ res.sort(function (a, b) { var aDate = Incito.ToDate(a.run_from).getTime(); var bDate = Incito.ToDate(b.run_from).getTime(); return bDate - aDate; }); if(res.length > 2){ $(configuration.targetElement).addClass('incito-many'); } else { $(configuration.targetElement).addClass('incito-few'); } for(i = 0; i < res.length; i++){ Incito.RenderPublishedCatalogToHtml(configuration, Incito.CustomLabeler(configuration, res[i])); } } else if(!err && res && configuration.fallback){ Incito.RenderFallback(configuration); } }); }; Incito.RenderSelectedCatalogs = function(configuration){ if(configuration.catalogList.length > 2){ $(configuration.targetElement).addClass('incito-many'); } else { $(configuration.targetElement).addClass('incito-few'); } for(i = 0; i < configuration.catalogList.length; i++) { $(configuration.targetElement).append('
'); $(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).hide(); } Incito.FetchCatalogs(configuration, function (err, res) { if (err){console.error(err)} else if(!err && res && res.length > 0){ res.sort(function (a, b) { var aDate = Incito.ToDate(a.run_from).getTime(); var bDate = Incito.ToDate(b.run_from).getTime(); return bDate - aDate; }); for(i = 0; i < res.length; i++){ a = Incito.CustomLabeler(configuration, res[i]); if(configuration.catalogList.includes(a.customLabel)){ Incito.RenderPublishedCatalogToHtml(configuration, a); } } for(i = 0; i < configuration.catalogList.length; i++){ if($(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).text() === ''){ if(configuration.catalogList[i] === 'current' || configuration.catalogList[i] === 'future'){ } else{ $(configuration.targetElement + ' #publication-'+configuration.catalogList[i]).show(); el = $(configuration.targetElement + ' #publication-'+configuration.catalogList[i])[0]; bs = new SGN.IncitoPublicationKit.Bootstrapper({ el: el, id: configuration.catalogList[i] }); bs.fetch(function (errB, resB) { if (errB){} if (!errB && resB) { Incito.RenderPublishedCatalogToHtml(configuration, Incito.CustomLabeler(configuration, resB.details)); } }); } } } } }); }; Incito.RenderFallback = function(configuration){ $(configuration.targetElement).addClass('incito-few'); $(configuration.targetElement).prepend(''); html = ''; if(configuration.fallback.href && configuration.fallback.href !== ''){ html += ''; } else { html += ''; } html += ''+configuration.fallback.title+''; html += '