Skip to content

Commit

Permalink
fix: persistent page-level targeting values (#129)
Browse files Browse the repository at this point in the history
## Description
This PR fixes an issue where old page-level targeting values are not
getting removed from `googletag.pubads()` when the ad config that gets
passed into `AdvertisingProvider` updates. What this mean is that when
targeting values are removed from your ad config, they still persist in
`googletag.pubads()`.

The fix is to call `pubads.clearTargeting()` to clear all previous
targeting values before calling `pubads.setTargeting(key, value)` to
pass in the updated targeting values.

---------

Co-authored-by: Stephen Gill <stephen.gill@adevinta.com>
  • Loading branch information
caffeinated-pixels and Stephen Gill committed May 17, 2023
1 parent 495aabf commit 791b9a6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Advertising.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ export default class Advertising {
this.defineOutOfPageSlots();
this.defineInterstitialSlot();
const entries = Object.entries(targeting);

// clear all previous targeting values before updating
pubads.clearTargeting();
// set or update page-level targeting
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
pubads.setTargeting(key, value);
Expand Down
1 change: 1 addition & 0 deletions src/Advertising.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ function setupGoogletag() {
return fakeSlot;
});
global.googletag.setTargeting = jest.fn().mockReturnValue(global.googletag);
global.googletag.clearTargeting = jest.fn().mockReturnValue(global.googletag);
global.googletag.addService = jest.fn().mockReturnValue(global.googletag);
global.googletag.pubads = jest.fn().mockReturnValue(global.googletag);
global.googletag.fakeSizeMapping = {
Expand Down
66 changes: 66 additions & 0 deletions src/__snapshots__/Advertising.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ Array [
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down Expand Up @@ -920,6 +931,17 @@ Object {
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down Expand Up @@ -1498,6 +1520,17 @@ Object {
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down Expand Up @@ -2333,6 +2366,17 @@ Array [
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down Expand Up @@ -3254,6 +3298,17 @@ Object {
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down Expand Up @@ -3832,6 +3887,17 @@ Object {
Array [
Object {
"addService": [MockFunction],
"clearTargeting": [MockFunction] {
"calls": Array [
Array [],
],
"results": Array [
Object {
"type": "return",
"value": [Circular],
},
],
},
"cmd": Object {
"push": [MockFunction] {
"calls": Array [
Expand Down

0 comments on commit 791b9a6

Please sign in to comment.