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

[MWPW-152968] mWeb - Passing ECID to Branch.io banner - Implementation #2567

Merged
merged 11 commits into from
Jul 22, 2024
Merged
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
24 changes: 22 additions & 2 deletions libs/blocks/mobile-app-banner/mobile-app-banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ async function getKey(product) {
return keyMatch[0]?.key;
}

async function getECID() {
narcis-radu marked this conversation as resolved.
Show resolved Hide resolved
mokimo marked this conversation as resolved.
Show resolved Hide resolved
let ecid = null;
if (window.alloy) {
await window.alloy('getIdentity').then((data) => {
narcis-radu marked this conversation as resolved.
Show resolved Hide resolved
ecid = data?.identity?.ECID;
}).catch((err) => window.lana.log(`Error fetching ECID: ${err}`, { tags: 'errorType=error,module=mobile-app-banner' }));
}
return ecid;
}

/* eslint-disable */
function branchInit(key) {
function branchInit(key, ecidVal) {
let initValue = false;
function initBranch() {
if (initValue) {
Expand Down Expand Up @@ -48,8 +58,16 @@ function branchInit(key) {
0
);
const privacyConsent = window.adobePrivacy?.hasUserProvidedConsent();
const isAndroid = navigator.userAgent.includes('Android');

const cookieGrp = window.adobePrivacy?.activeCookieGroups();
const performanceCookieConsent = cookieGrp.includes('C0002');
const advertisingCookieConsent = cookieGrp.includes('C0004');

if (performanceCookieConsent && advertisingCookieConsent && isAndroid) branch.setBranchViewData({ data: { ecid: ecidVal }});
branch.init(key, { tracking_disabled: !privacyConsent });
}

['adobePrivacy:PrivacyConsent', 'adobePrivacy:PrivacyReject', 'adobePrivacy:PrivacyCustom']
.forEach((event) => {
window.addEventListener(event, initBranch);
Expand All @@ -63,5 +81,7 @@ export default async function init(el) {
const classListArray = Array.from(el.classList);
const product = classListArray.find((token) => token.startsWith('product-')).split('-')[1];
const key = await getKey(product);
if (key) branchInit(key);
if (!key) return;
const ecid = await getECID();
branchInit(key, ecid);
}
34 changes: 33 additions & 1 deletion test/blocks/mobile-app-banner/mobile-app-banner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ describe('mobile-app-banner', () => {
});

it('should init by adding branchio script', async () => {
window.adobePrivacy = { hasUserProvidedConsent: () => true };
window.adobePrivacy = {
hasUserProvidedConsent: () => true,
activeCookieGroups: () => ['C0002', 'C0004'],
};
const userAgentStub = sinon.stub(navigator, 'userAgent').get(() => 'Android');
const module = await import('../../../libs/blocks/mobile-app-banner/mobile-app-banner.js');
const banner = document.body.querySelector('.mobile-app-banner.product-test');
await module.default(banner);
Expand All @@ -63,5 +67,33 @@ describe('mobile-app-banner', () => {
if (scriptTag.getAttribute('src') !== null) scriptSrcs.push(scriptTag.getAttribute('src'));
});
expect(scriptSrcs).to.include('https://cdn.branch.io/branch-latest.min.js');
userAgentStub.restore();
});

it('should fetch ecid from alloy and return if event is dispatched twice', async () => {
window.adobePrivacy = {
hasUserProvidedConsent: () => true,
activeCookieGroups: () => ['C0002', 'C0004'],
};
window.alloy = () => {};
const alloyStub = sinon.stub(window, 'alloy').callsFake((command) => {
if (command === 'getIdentity') {
return Promise.resolve({ identity: { ECID: 'test-ecid' } });
}
return 'test-ecid';
});
const module = await import('../../../libs/blocks/mobile-app-banner/mobile-app-banner.js');
const banner = document.body.querySelector('.mobile-app-banner.product-test');
await module.default(banner);
window.dispatchEvent(new CustomEvent('adobePrivacy:PrivacyConsent'));
await delay(0);
const scriptTags = document.querySelectorAll('head > script');
const scriptSrcs = [];
scriptTags.forEach((scriptTag) => {
if (scriptTag.getAttribute('src') !== null) scriptSrcs.push(scriptTag.getAttribute('src'));
});
window.dispatchEvent(new CustomEvent('adobePrivacy:PrivacyConsent'));
expect(scriptSrcs).to.include('https://cdn.branch.io/branch-latest.min.js');
alloyStub.restore();
});
});
Loading