Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cmp_interaction_tracking.js #185

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions extensions/cmp_interaction_tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@

function isAfterCMP() {
const hasCMPAfterCookie = window.utag.data['cp.utag_main_cmp_after'] ? (window.utag.data['cp.utag_main_cmp_after'] === 'true') : false;
const hasCMPAfterCookie_subdomain = window.utag.data['cp.utag_main_cmp_after_sub'] ? (window.utag.data['cp.utag_main_cmp_after_sub'] === 'true') : false;
const defaultVendorList = 'adobe_cmp,';
const hasVendors = !!window.utag.data.consentedVendors && window.utag.data.consentedVendors !== defaultVendorList;
const hasVendors_subdomain = !!window.utag.data['cp.cm_cv_list'] && window.utag.data['cp.cm_cv_list'] !== defaultVendorList;

// sportbild.bild.de needs special treatment because of sub-domain issues.
const subdomains = [
'sportbild.bild.de',
'm.sportbild.bild.de',
'shop.bild.de',
'angebot.bild.de',
'shopping.welt.de',
Expand All @@ -113,7 +116,7 @@
// sportbild.bild.de, shop.bild.de, offerpages needs special treatment because of sub-domain issues.
if ((window.utag.data['dom.domain']) && subdomains.indexOf(window.utag.data['dom.domain']) !== -1){
// hasCMPAfterCookie cannot be used here because it shares cookie with base domain
return hasVendors;
return hasCMPAfterCookie_subdomain || hasVendors_subdomain;
} else {
return hasCMPAfterCookie || hasVendors;
}
Expand Down Expand Up @@ -142,7 +145,9 @@
// Calling setScrollDepthProperties() will make the current page trackable as the _ppvPreviousPage of the next page view.
window.cmp._scrollDepthObj.setScrollDepthProperties(window.cmp);
}
window.cmp._campaignObj.setCampaignVariables(window.cmp, true);
if (window.cmp && window.cmp._campaignObj) {
window.cmp._campaignObj.setCampaignVariables(window.cmp, true);
}
}

function sendFirstPageViewEvent() {
Expand All @@ -153,16 +158,20 @@
}
}

function onMessageChoiceSelect(id, eventType) {
function onMessageChoiceSelect(messageType, id, eventType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the messageType added here used somewhere in the function? if yes, we would need to add it in our tests as well.

if (CONSENT_MESSAGE_EVENTS[eventType]) {
window.utag.data['cmp_events'] = CONSENT_MESSAGE_EVENTS[eventType];
exportedFunctions.sendLinkEvent(CONSENT_MESSAGE_EVENTS[eventType]);

if (eventType === 11 || eventType === 13) {
if ((eventType === 11 && (window.utag.data['dom.domain'] && window.utag.data['dom.domain'].includes('sportbild.bild.de'))) || (eventType === 13 && (window.utag.data['dom.domain'] && window.utag.data['dom.domain'].includes('sportbild.bild.de'))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ((eventType === 11 || eventType === 13) && window.utag.data['dom.domain'] && window.utag.data['dom.domain'].includes('sportbild.bild.de'))

{
window.utag.loader.SC('utag_main', {'cmp_after_sub': 'true'});
window.utag.data['cp.utag_main_cmp_after_sub'] = 'true';
} else if (eventType !== 12 ) {
window.utag.loader.SC('utag_main', {'cmp_after': 'true'});
window.utag.data['cp.utag_main_cmp_after'] = 'true';
}

if (eventType === 11) {
exportedFunctions.onUserConsent();
}
Expand All @@ -174,8 +183,13 @@
window.utag.data['cmp_events'] = eventType.purposeConsent ? (eventType.purposeConsent === 'all' ? PRIVACY_MANAGER_EVENTS.ACCEPT_ALL : PRIVACY_MANAGER_EVENTS.SAVE_AND_EXIT) : PRIVACY_MANAGER_EVENTS[eventType];
exportedFunctions.sendLinkEvent(window.utag.data['cmp_events']);
// Set cookie for first page view tracking.
window.utag.loader.SC('utag_main', {'cmp_after': 'true'});
window.utag.data['cp.utag_main_cmp_after'] = 'true';
if (window.utag.data['dom.domain'] && window.utag.data['dom.domain'].includes('sportbild.bild.de')){
window.utag.loader.SC('utag_main', {'cmp_after_sub': 'true'});
window.utag.data['cp.utag_main_cmp_after_sub'] = 'true';
}else{
window.utag.loader.SC('utag_main', {'cmp_after': 'true'});
window.utag.data['cp.utag_main_cmp_after'] = 'true';
}
}
}

Expand Down
32 changes: 15 additions & 17 deletions tests/cmp_interaction_tracking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,80 +322,78 @@ describe('CMP Interaction Tracking', () => {
});
});

describe('onMessageChoiceSelect(id, eventType)', () => {
describe('onMessageChoiceSelect(messageType, id, eventType)', () => {
beforeEach(() => {
jest.spyOn(cmpInteractionTracking, 'sendLinkEvent').mockImplementation();
jest.spyOn(cmpInteractionTracking, 'onUserConsent').mockImplementation();
});

it('should set correct utag.data properties when user gives consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('any-id', 11);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'any-id', 11);
expect(window.utag.data).toEqual({
'cmp_events': 'cm_accept_all',
'cp.utag_main_cmp_after': 'true'
});
});

it('should call sendLinkEvent with correct argument when user gives consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 11);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 11);
expect(cmpInteractionTracking.sendLinkEvent).toHaveBeenLastCalledWith('cm_accept_all');
});

it('should set correct utag.data properties when user opens privacy manager', () => {
cmpInteractionTracking.onMessageChoiceSelect('any-id', 12);
expect(window.utag.data).toEqual({
'cmp_events': 'cm_show_privacy_manager'
});
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'any-id', 12);
expect(window.utag.data.cmp_events).toBe('cm_show_privacy_manager');
});

it('should call sendLinkEvent with correct argument when user opens privacy manager', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 12);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 12);
expect(cmpInteractionTracking.sendLinkEvent).toHaveBeenLastCalledWith('cm_show_privacy_manager');
});

it('should set correct utag.data properties when user declines consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('any-id', 13);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'any-id', 13);
expect(window.utag.data).toEqual({
'cmp_events': 'cm_reject_all',
'cp.utag_main_cmp_after': 'true'
});
});

it('should call sendLinkEvent with correct argument when user declines consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 13);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 13);
expect(cmpInteractionTracking.sendLinkEvent).toHaveBeenLastCalledWith('cm_reject_all');
});

it('should NOT call sendLinkEvent when called with wrong event type', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 999);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 999);
expect(cmpInteractionTracking.sendLinkEvent).not.toHaveBeenCalled();
});

it('should set utag_main_cmp_after cookie to true when user gives consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 11);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 11);
expect(window.utag.loader.SC).toHaveBeenCalledWith('utag_main', {'cmp_after': 'true'});
expect(window.utag.data['cp.utag_main_cmp_after']).toBe('true');
});

it('should set utag_main_cmp_after cookie to true when user declines consent', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 13);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 13);
expect(window.utag.loader.SC).toHaveBeenCalledWith('utag_main', {'cmp_after': 'true'});
expect(window.utag.data['cp.utag_main_cmp_after']).toBe('true');
});

it('should NOT set utag_main_cmp_after cookie when user opens privacy manager', () => {
cmpInteractionTracking.onMessageChoiceSelect('test', 12);
expect(window.utag.loader.SC).not.toHaveBeenCalledWith('utag_main', {'cmp_after': 'true'});
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'test', 12);
expect(window.utag.loader.SC).not.toHaveBeenCalledWith(['utag_main', expect.objectContaining({'cmp_after': 'true'})]);
expect(window.utag.data['cp.utag_main_cmp_after']).toBeUndefined();
});

it('should call onUserConsent() when user has given consent', function () {
cmpInteractionTracking.onMessageChoiceSelect('any-id', 11);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'any-id', 11);
expect(cmpInteractionTracking.onUserConsent).toHaveBeenCalled();
});

it('should not call onUserConsent() when user has NOT given consent', function () {
cmpInteractionTracking.onMessageChoiceSelect('any-id', 12);
cmpInteractionTracking.onMessageChoiceSelect('any-message', 'any-id', 12);
expect(cmpInteractionTracking.onUserConsent).not.toHaveBeenCalled();
});
});
Expand Down