Skip to content

Commit

Permalink
- add module name to log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksatr committed Nov 30, 2021
1 parent 046948c commit ebf0a21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions modules/viewability.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { logWarn, logInfo, isStr, isFn, triggerPixel, insertHtmlIntoIframe } fro
import { getGlobal } from '../src/prebidGlobal.js';
import find from 'core-js-pure/features/array/find.js';

export const MODULE_NAME = 'viewability';

export function init() {
(getGlobal()).viewability = {
startMeasurement: startMeasurement,
Expand All @@ -15,7 +17,7 @@ const observers = {};

function isValid(vid, element, tracker, criteria) {
if (!element) {
logWarn('provide an html element to track');
logWarn(`${MODULE_NAME}: provide an html element to track`);
return false;
}

Expand All @@ -25,17 +27,17 @@ function isValid(vid, element, tracker, criteria) {
(tracker.method === 'callback' && isFn(tracker.value)));

if (!validTracker) {
logWarn('invalid tracker', tracker);
logWarn(`${MODULE_NAME}: invalid tracker`, tracker);
return false;
}

if (!criteria || !criteria.inViewThreshold || !criteria.timeInView) {
logWarn('missing criteria', criteria);
logWarn(`${MODULE_NAME}: missing criteria`, criteria);
return false;
}

if (!vid || observers[vid]) {
logWarn('provide an unregistered vid', vid);
logWarn(`${MODULE_NAME}: provide an unregistered vid`, vid);
return false;
}

Expand Down Expand Up @@ -76,7 +78,7 @@ export function startMeasurement(vid, element, tracker, criteria) {
switch (tracker.method) {
case 'img':
triggerPixel(tracker.value, () => {
logInfo('viewability pixel fired', tracker.value);
logInfo(`${MODULE_NAME}: viewability pixel fired`, tracker.value);
});
break;
case 'js':
Expand Down Expand Up @@ -109,7 +111,7 @@ export function startMeasurement(vid, element, tracker, criteria) {
*/
export function stopMeasurement(vid) {
if (!vid || !observers[vid]) {
logWarn('provide a registered vid', vid);
logWarn(`${MODULE_NAME}: provide a registered vid`, vid);
return;
}

Expand Down
10 changes: 5 additions & 5 deletions test/spec/modules/viewability_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,28 @@ describe('viewability test', () => {

viewability.startMeasurement('4', {}, { method: 'js', value: 'http://my.tracker/123.js' }, { inViewThreshold: 0.5, timeInView: 1000 });
expect(logWarnSpy.callCount).to.equal(1);
expect(logWarnSpy.calledWith('provide an unregistered vid', '4')).to.equal(true);
expect(logWarnSpy.calledWith(`${viewability.MODULE_NAME}: provide an unregistered vid`, '4')).to.equal(true);
});

it('should check for valid criteria', () => {
let logWarnSpy = sandbox.spy(utils, 'logWarn');
viewability.startMeasurement('5', {}, { method: 'js', value: 'http://my.tracker/123.js' }, { timeInView: 1000 });
expect(logWarnSpy.callCount).to.equal(1);
expect(logWarnSpy.calledWith('missing criteria', { timeInView: 1000 })).to.equal(true);
expect(logWarnSpy.calledWith(`${viewability.MODULE_NAME}: missing criteria`, { timeInView: 1000 })).to.equal(true);
});

it('should check for valid tracker', () => {
let logWarnSpy = sandbox.spy(utils, 'logWarn');
viewability.startMeasurement('6', {}, { method: 'callback', value: 'string' }, { inViewThreshold: 0.5, timeInView: 1000 });
expect(logWarnSpy.callCount).to.equal(1);
expect(logWarnSpy.calledWith('invalid tracker', { method: 'callback', value: 'string' })).to.equal(true);
expect(logWarnSpy.calledWith(`${viewability.MODULE_NAME}: invalid tracker`, { method: 'callback', value: 'string' })).to.equal(true);
});

it('should check if element provided', () => {
let logWarnSpy = sandbox.spy(utils, 'logWarn');
viewability.startMeasurement('7', undefined, { method: 'js', value: 'http://my.tracker/123.js' }, { timeInView: 1000 });
expect(logWarnSpy.callCount).to.equal(1);
expect(logWarnSpy.calledWith('provide an html element to track')).to.equal(true);
expect(logWarnSpy.calledWith(`${viewability.MODULE_NAME}: provide an html element to track`)).to.equal(true);
});
});

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('viewability test', () => {
let logWarnSpy = sandbox.spy(utils, 'logWarn');
viewability.stopMeasurement('100');
expect(logWarnSpy.callCount).to.equal(1);
expect(logWarnSpy.calledWith('provide a registered vid', '100')).to.equal(true);
expect(logWarnSpy.calledWith(`${viewability.MODULE_NAME}: provide a registered vid`, '100')).to.equal(true);
});
});

Expand Down

0 comments on commit ebf0a21

Please sign in to comment.