Skip to content

Commit

Permalink
refactor how adId pass to click event, fix issue when there is inner …
Browse files Browse the repository at this point in the history
…html element inside <a> tag (#103)
  • Loading branch information
wqi1972 authored Jun 4, 2020
1 parent 40b352d commit 96816e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
25 changes: 8 additions & 17 deletions src/nativeTrackerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ export function newNativeTrackerManager(win) {
return adId || '';
}

function readAdIdFromEvent(event) {
let adId =
event &&
event.target &&
event.target.attributes &&
event.target.attributes[AD_DATA_ADID_ATTRIBUTE] &&
event.target.attributes[AD_DATA_ADID_ATTRIBUTE].value;

return adId || '';
}

function loadClickTrackers(event) {
let adId = readAdIdFromEvent(event);
function loadClickTrackers(event, adId) {
fireTracker(adId, 'click');
}

Expand All @@ -57,7 +45,10 @@ export function newNativeTrackerManager(win) {
adElements = adElements || findAdElements(AD_ANCHOR_CLASS_NAME);

for (let i = 0; i < adElements.length; i++) {
adElements[i].addEventListener('click', listener, true);
let adId = readAdIdFromSingleElement(adElements[i]);
adElements[i].addEventListener('click', function(event) {
listener(event, adId);
}, true);
}
}

Expand Down Expand Up @@ -88,16 +79,16 @@ export function newNativeTrackerManager(win) {
}
const boundedLoadMobileClickTrackers = loadMobileClickTrackers.bind(null, clickTrackers);
attachClickListeners(false, boundedLoadMobileClickTrackers);

(impTrackers || []).forEach(triggerPixel);
}
nativeAssetManager.loadMobileAssets(targetingData, cb);
} else {
let parsedUrl = parseUrl(targetingData && targetingData.pubUrl);
publisherDomain = parsedUrl.protocol + '://' + parsedUrl.host;

let adElements = findAdElements(AD_ANCHOR_CLASS_NAME);

nativeAssetManager.loadAssets(
readAdIdFromElement(adElements),
attachClickListeners
Expand Down
17 changes: 6 additions & 11 deletions test/spec/nativeTrackerManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('nativeTrackerManager', function () {
describe('load startTrackers', function () {
let mockWin;
let consoleWarn;

let tagData = {
pubUrl: 'http://example.com'
};
Expand Down Expand Up @@ -70,13 +70,6 @@ describe('nativeTrackerManager', function () {
},
addEventListener: ((type, listener, capture) => {
listener({
target: {
attributes: {
pbAdId: {
value: 'ad123'
}
}
}
})
})
}];
Expand All @@ -96,16 +89,18 @@ describe('nativeTrackerManager', function () {
expect(trimPort(postMessageTargetDomain)).to.equal(tagData.pubUrl);
});

it('should verify the warning message was executed', function() {
it('should verify 2 warning messages (one for impression, one for click) was executed', function() {
mockWin.document.getElementsByClassName = () => [{
addEventListener: ((type, listener, capture) => {
listener({
})
})
}];

let nativeTracker = new newNativeTrackerManager(mockWin);
nativeTracker.startTrackers(tagData);

expect(consoleWarn.callCount).to.equal(1);
expect(consoleWarn.callCount).to.equal(2);
});
});
});
});

0 comments on commit 96816e0

Please sign in to comment.