Skip to content

Commit

Permalink
Category exclusion feature added for page level.
Browse files Browse the repository at this point in the history
  • Loading branch information
larryaubstore committed Jul 10, 2013
1 parent d23735d commit b7020c2
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

set tabstop=4
set shiftwidth=4
set expandtab
17 changes: 16 additions & 1 deletion jquery.dfp.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
'Query': URLTargets.Query,
'Domain': window.location.host
},
'setCategoryExclusion': '',
'enableSingleRequest': true,
'collapseEmptyDivs': 'original',
'targetPlatform': 'web',
Expand Down Expand Up @@ -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());
}
});
}

Expand Down Expand Up @@ -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();
}
Expand Down
265 changes: 220 additions & 45 deletions tests/spec/jquerySpec.dfp.js
Original file line number Diff line number Diff line change
@@ -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("<div id=\"testdiv\"></div>" +
"<div class=\"adunit\" id=\"Ad_unit_id\"" +
" data-exclusions=\"firstcategory, secondcategory\"></div>");

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( "<div class=\"adunit\" id=\"Ad_unit_id\"" +
" data-exclusions=\"firstcategory, secondcategory\"></div>");

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("<div class=\"adunit\" id=\"Ad_unit_id\"" +
" data-exclusions=\"firstcategory, \"></div>");

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");
});



});


Expand Down

0 comments on commit b7020c2

Please sign in to comment.