Skip to content

Commit

Permalink
[MWPW-152968] mWeb - Passing ECID to Branch.io banner - Implementation (
Browse files Browse the repository at this point in the history
#2567)

* initial commit

* initial commit

* initial commit

* final changes

* final changes

* increased code coverage

* review changes

* alloy test case

* added catch statement

* error type

---------

Co-authored-by: Drashti Modasara <drashti@drashtis-mbp.corp.adobe.com>
Co-authored-by: Drashti Modasara <drashti@Drashtis-MacBook-Pro.local>
Co-authored-by: Drashti Modasara <drashti@drashtis-mbp.macromedia.com>
  • Loading branch information
4 people authored Jul 22, 2024
1 parent 2dbd04c commit 6943446
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
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() {
let ecid = null;
if (window.alloy) {
await window.alloy('getIdentity').then((data) => {
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();
});
});

0 comments on commit 6943446

Please sign in to comment.