-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
1,717 additions
and
664 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@segment/analytics-consent-tools': major | ||
'@segment/analytics-consent-wrapper-onetrust': major | ||
--- | ||
|
||
Add opt-out consent-model support, and use opt-out by default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
...n-tests/public/consent-tools-vanilla.html → .../public/consent-tools-vanilla-opt-in.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<script src="dist/consent-tools-vanilla.bundle.js"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Hello World - Serving Analytics</h1> | ||
<h2>Please Check Network tab</h2> | ||
<p>This page can used as playground or run by webdriver.io</p> | ||
<script src="/public/dist/consent-tools-vanilla-opt-in.bundle.js"></script> | ||
</body> | ||
|
||
</html> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
packages/consent/consent-tools-integration-tests/public/consent-tools-vanilla-opt-out.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<body> | ||
<h1>Hello World - Serving Analytics (Consent Tools Vanilla Opt Out)</h1> | ||
<h2>Please Check Network tab</h2> | ||
<p>This page can used as playground or run by webdriver.io</p> | ||
<script src="/public/dist/consent-tools-vanilla-opt-out.bundle.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...nt/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla-opt-in/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { AnalyticsBrowser } from '@segment/analytics-next' | ||
import { initMockConsentManager } from '../helpers/mock-cmp' | ||
import { withMockCMP } from '../helpers/mock-cmp-wrapper' | ||
|
||
initMockConsentManager({ consentModel: 'opt-in' }) | ||
|
||
const analytics = new AnalyticsBrowser() | ||
|
||
// for testing | ||
;(window as any).analytics = analytics | ||
|
||
withMockCMP(analytics).load({ | ||
writeKey: 'foo', | ||
}) |
17 changes: 17 additions & 0 deletions
17
...t/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla-opt-out/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AnalyticsBrowser } from '@segment/analytics-next' | ||
import { initMockConsentManager } from '../helpers/mock-cmp' | ||
import { withMockCMP } from '../helpers/mock-cmp-wrapper' | ||
|
||
initMockConsentManager({ | ||
consentModel: 'opt-out', | ||
}) | ||
|
||
const analytics = new AnalyticsBrowser() | ||
|
||
withMockCMP(analytics).load( | ||
{ | ||
writeKey: '9lSrez3BlfLAJ7NOChrqWtILiATiycoc', | ||
}, | ||
{ initialPageview: true } | ||
) | ||
;(window as any).analytics = analytics |
17 changes: 0 additions & 17 deletions
17
...s/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
...ages/consent/consent-tools-integration-tests/src/page-bundles/helpers/mock-cmp-wrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createWrapper, resolveWhen } from '@segment/analytics-consent-tools' | ||
|
||
export const withMockCMP = (analyticsInstance: any) => | ||
createWrapper({ | ||
enableDebugLogging: true, | ||
shouldLoadWrapper: async () => { | ||
await resolveWhen(() => window.MockCMP.isLoaded, 500) | ||
}, | ||
getCategories: () => window.MockCMP.getCategories(), | ||
registerOnConsentChanged: (fn) => { | ||
window.MockCMP.onConsentChange(fn) | ||
}, | ||
shouldLoadSegment: async (ctx) => { | ||
if (window.MockCMP.consentModel === 'opt-out') { | ||
// if opt out, load immediately | ||
return ctx.load({ consentModel: 'opt-out' }) | ||
} | ||
await window.MockCMP.waitForAlertBoxClose() | ||
ctx.load({ consentModel: 'opt-in' }) | ||
}, | ||
})(analyticsInstance) |
109 changes: 109 additions & 0 deletions
109
packages/consent/consent-tools-integration-tests/src/page-bundles/helpers/mock-cmp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const constants = { | ||
giveConsentId: 'give-consent', | ||
denyConsentId: 'deny-consent', | ||
} | ||
export type MockConsentManager = ReturnType<typeof initMockConsentManager> | ||
|
||
declare global { | ||
interface Window { | ||
MockCMP: MockConsentManager | ||
} | ||
} | ||
type ConsentChangeFn = (categories: Record<string, boolean>) => void | ||
|
||
/** | ||
* This is a mock consent manager that simulates a consent manager that is loaded asynchronously. | ||
* Similar to OneTrust, TrustArc, etc. | ||
* sets a global `window.MockCMP` object that can be used to interact with the mock consent manager. | ||
*/ | ||
export const initMockConsentManager = (settings: { consentModel: string }) => { | ||
const isOptIn = settings.consentModel === 'opt-in' | ||
// if opt-in is true, all categories are set to true by default | ||
let categories = { | ||
FooCategory1: isOptIn, | ||
FooCategory2: isOptIn, | ||
} | ||
console.log('initMockConsentManager', settings, categories) | ||
|
||
let onConsentChange = (_categories: Record<string, boolean>) => | ||
undefined as void | ||
|
||
const createAlertBox = () => { | ||
const container = document.createElement('div') | ||
container.id = 'alert-box' | ||
container.innerHTML = ` | ||
<button id="${constants.giveConsentId}">Give consent</button> | ||
<button id="${constants.denyConsentId}">Deny consent</button> | ||
` | ||
return container | ||
} | ||
|
||
const alertBox = createAlertBox() | ||
let loaded = false | ||
setTimeout(() => { | ||
loaded = true | ||
mockCMPPublicAPI.openAlertBox() | ||
}, 300) | ||
|
||
/** | ||
* similar to window.OneTrust | ||
*/ | ||
const mockCMPPublicAPI = { | ||
get isLoaded() { | ||
return loaded | ||
}, | ||
get consentModel() { | ||
return settings.consentModel | ||
}, | ||
setCategories: (newCategories: Record<string, boolean>) => { | ||
categories = { ...categories, ...newCategories } | ||
onConsentChange(categories) | ||
return categories | ||
}, | ||
waitForAlertBoxClose() { | ||
return new Promise<void>((resolve) => { | ||
document | ||
.getElementById('give-consent')! | ||
.addEventListener('click', () => { | ||
resolve() | ||
}) | ||
|
||
document | ||
.getElementById('deny-consent')! | ||
.addEventListener('click', () => { | ||
resolve() | ||
}) | ||
}) | ||
}, | ||
getCategories: () => categories, | ||
openAlertBox: () => { | ||
document.body.appendChild(alertBox) | ||
document | ||
.getElementById(constants.giveConsentId)! | ||
.addEventListener('click', () => { | ||
mockCMPPublicAPI.setCategories({ | ||
FooCategory1: true, | ||
FooCategory2: true, | ||
}) | ||
mockCMPPublicAPI.closeAlertBox() | ||
}) | ||
document | ||
.getElementById(constants.denyConsentId)! | ||
.addEventListener('click', () => { | ||
mockCMPPublicAPI.setCategories({ | ||
FooCategory1: false, | ||
FooCategory2: false, | ||
}) | ||
mockCMPPublicAPI.closeAlertBox() | ||
}) | ||
}, | ||
closeAlertBox: () => { | ||
document.body.removeChild(alertBox) | ||
}, | ||
onConsentChange: (fn: ConsentChangeFn) => { | ||
onConsentChange = fn | ||
}, | ||
} | ||
window.MockCMP = mockCMPPublicAPI | ||
return mockCMPPublicAPI | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.