diff --git a/.vimrc b/.vimrc new file mode 100644 index 0000000..31c5d78 --- /dev/null +++ b/.vimrc @@ -0,0 +1,4 @@ + +set tabstop=4 +set shiftwidth=4 +set expandtab diff --git a/jquery.dfp.js b/jquery.dfp.js index 72bfbba..c365bae 100644 --- a/jquery.dfp.js +++ b/jquery.dfp.js @@ -76,6 +76,7 @@ 'Query': URLTargets.Query, 'Domain': window.location.host }, + 'setCategoryExclusion': '', 'enableSingleRequest': true, 'collapseEmptyDivs': 'original', 'targetPlatform': 'web', @@ -157,8 +158,12 @@ var exclusions = $adUnit.data("exclusions"); if (exclusions) { var exclusionsGroup = exclusions.split(','); + var valueTrimmed; $.each(exclusionsGroup, function (k, v) { - googleAdUnit.setCategoryExclusion(v.trim()); + valueTrimmed = v.trim(); + if(valueTrimmed.length > 0) { + googleAdUnit.setCategoryExclusion(v.trim()); + } }); } @@ -210,6 +215,16 @@ $.each(dfpOptions.setTargeting, function (k, v) { window.googletag.pubads().setTargeting(k, v); }); + if (dfpOptions.setCategoryExclusion.length > 0) { + var exclusionsGroup = dfpOptions.setCategoryExclusion.split(','); + var valueTrimmed; + $.each(exclusionsGroup, function (k, v) { + valueTrimmed = v.trim(); + if(valueTrimmed.length > 0) { + window.googletag.pubads().setCategoryExclusion(v.trim()); + } + }); + } if (dfpOptions.collapseEmptyDivs === true || dfpOptions.collapseEmptyDivs === 'original') { window.googletag.pubads().collapseEmptyDivs(); } diff --git a/tests/spec/jquerySpec.dfp.js b/tests/spec/jquerySpec.dfp.js index 3104383..f671d34 100644 --- a/tests/spec/jquerySpec.dfp.js +++ b/tests/spec/jquerySpec.dfp.js @@ -1,57 +1,232 @@ describe("jquery.dfp.js unit tests", function() { - beforeEach(function() { - jQuery("#testdiv").remove(); - }); - - it("Category exclusion", function() { - var mockAdunit = { - setCategoryExclusion: function(param) { - } - }; - - spyOn(mockAdunit, "setCategoryExclusion").andCallThrough(); - - var myGoogletag = {}; - myGoogletag.defineSlot = function() { - return { - addService: function() { - return mockAdunit; - } - } - }; - - myGoogletag.pudads = function() {}; - myGoogletag.cmd = {}; - myGoogletag.cmd.push = function () { }; - - jQuery("body").append("
" + - ""); - - jQuery.dfp({ - dfpID: 'xxxxxxxxx', - googletag: myGoogletag + beforeEach(function() { + jQuery(".adunit").remove(); }); - var timeoutExpired = false; + it("Category exclusion (ad unit)", function() { + var mockAdunit = { + setCategoryExclusion: function(param) { } + }; + spyOn(mockAdunit, "setCategoryExclusion").andCallThrough(); - waitsFor(function() { - return timeoutExpired; - }, "Timeout neved expired", 2000); + window.googletag = {}; + window.googletag.cmd = {}; + window.googletag.pubads = function () {}; + + // PUSH 1 = $adCollection loop + // PUSH 2 = DFP config options + var pushCounter = 1; + window.googletag.cmd.push = function(callback) { - runs(function() { - expect(mockAdunit.setCategoryExclusion).toHaveBeenCalled(); - expect(mockAdunit.setCategoryExclusion.callCount).toEqual(2); - expect(mockAdunit.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); - expect(mockAdunit.setCategoryExclusion.calls[1].args[0]).toEqual("secondcategory"); + if(pushCounter == 1) { + callback(); + } + pushCounter++; + }; + + + googletag.defineSlot = function() { + return { + addService: function() { + return mockAdunit; + } + } + }; + + jQuery("body").append( ""); + + jQuery.dfp({ + dfpID: 'xxxxxxxxx' + }); + + + expect(mockAdunit.setCategoryExclusion).toHaveBeenCalled(); + expect(mockAdunit.setCategoryExclusion.callCount).toEqual(2); + expect(mockAdunit.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); + expect(mockAdunit.setCategoryExclusion.calls[1].args[0]).toEqual("secondcategory"); + + }); + + it("Category exclusion (ad unit) extra comma", function() { + var mockAdunit = { + setCategoryExclusion: function(param) { } + }; + + spyOn(mockAdunit, "setCategoryExclusion").andCallThrough(); + + window.googletag = {}; + window.googletag.cmd = {}; + window.googletag.pubads = function () {}; + + // PUSH 1 = $adCollection loop + // PUSH 2 = DFP config options + var pushCounter = 1; + window.googletag.cmd.push = function(callback) { + + if(pushCounter == 1) { + callback(); + } + pushCounter++; + }; + + + googletag.defineSlot = function() { + return { + addService: function() { + return mockAdunit; + } + } + }; + + jQuery("body").append(""); + + var test = jQuery(".adunit"); + + jQuery.dfp({ + dfpID: 'xxxxxxxxx' + }); + + + expect(mockAdunit.setCategoryExclusion).toHaveBeenCalled(); + expect(mockAdunit.setCategoryExclusion.callCount).toEqual(1); + expect(mockAdunit.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); + + }); + + it("Category exclusion (page)", function() { + + var pushCounter = 1; + window.googletag = {}; + window.googletag.enableServices = function () {}; + googletag.cmd = {}; + + // PUSH 1 = DFP config options + window.googletag.cmd.push = function(callback) { + + if(pushCounter == 1) { + callback(); + } + pushCounter++; + }; + + + var mock = {}; + mock.setCategoryExclusion = function(param) { + }; + + window.googletag.pubads = function () + { + return { + enableSingleRequest: function () {}, + setTargeting: function () {}, + collapseEmptyDivs: function () {}, + setCategoryExclusion: mock.setCategoryExclusion + } + }; + + spyOn(mock, "setCategoryExclusion").andCallThrough(); + + jQuery.dfp({ + dfpID: 'xxxxxxxxx', + setCategoryExclusion: "firstcategory, secondcategory" + }); + + expect(mock.setCategoryExclusion).toHaveBeenCalled(); + expect(mock.setCategoryExclusion.callCount).toEqual(2); + expect(mock.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); + expect(mock.setCategoryExclusion.calls[1].args[0]).toEqual("secondcategory"); + }); + + it("Category exclusion (page) with extra comma", function() { + + var pushCounter = 1; + window.googletag = {}; + window.googletag.enableServices = function () {}; + googletag.cmd = {}; + + // PUSH 1 = DFP config options + window.googletag.cmd.push = function(callback) { + + if(pushCounter == 1) { + callback(); + } + pushCounter++; + }; + + + var mock = {}; + mock.setCategoryExclusion = function(param) { + }; + + window.googletag.pubads = function () + { + return { + enableSingleRequest: function () {}, + setTargeting: function () {}, + collapseEmptyDivs: function () {}, + setCategoryExclusion: mock.setCategoryExclusion + } + }; + + spyOn(mock, "setCategoryExclusion").andCallThrough(); + jQuery.dfp({ + dfpID: 'xxxxxxxxx', + setCategoryExclusion: "firstcategory," + }); + + expect(mock.setCategoryExclusion).toHaveBeenCalled(); + expect(mock.setCategoryExclusion.callCount).toEqual(1); + expect(mock.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); }); - setTimeout(function() { - timeoutExpired = true; - }, 750); - }); + it("Category exclusion (page) one value and no commas", function() { + + var pushCounter = 1; + window.googletag = {}; + window.googletag.enableServices = function () {}; + googletag.cmd = {}; + + // PUSH 1 = DFP config options + window.googletag.cmd.push = function(callback) { + + if(pushCounter == 1) { + callback(); + } + pushCounter++; + }; + + + var mock = {}; + mock.setCategoryExclusion = function(param) { + }; + + window.googletag.pubads = function () + { + return { + enableSingleRequest: function () {}, + setTargeting: function () {}, + collapseEmptyDivs: function () {}, + setCategoryExclusion: mock.setCategoryExclusion + } + }; + + spyOn(mock, "setCategoryExclusion").andCallThrough(); + jQuery.dfp({ + dfpID: 'xxxxxxxxx', + setCategoryExclusion: "firstcategory" + }); + + expect(mock.setCategoryExclusion).toHaveBeenCalled(); + expect(mock.setCategoryExclusion.callCount).toEqual(1); + expect(mock.setCategoryExclusion.calls[0].args[0]).toEqual("firstcategory"); + }); + + + });