all files / src/ index.js

75.86% Statements 44/58
63.33% Branches 19/30
80% Functions 12/15
75.86% Lines 44/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                            10×                                   10× 10×   10× 10× 10× 10×   10× 10×                                       10×   10× 10×             10× 10×     10×       10× 10× 10×   10×       10× 10× 10×   10×   10×                         10× 10× 10×         10×           10×   10× 10× 10× 10×         10×                            
import './script-public-path';
import { AC_PROVIDERS, IAB_VENDORS, SESSION_COOKIES } from './shared/consts';
import GeoManager, { ensureGeoCookie } from './shared/GeoManager';
import UserSignalMechanism from './ccpa/UserSignalMechanism';
import { communicationService } from './shared/communication';
import { debug } from './shared/utils';
import { oneTrust } from './onetrust';
 
export const DEFAULT_OPTIONS = {
    sessionCookies: SESSION_COOKIES, // array of sessionCookies with extension times
    beaconCookieName: null,
    cookieName: null, // use default cookie name
    cookieExpiration: null, // use default
    cookieRejectExpiration: null,
    country: null, // country code
    countriesRequiringPrompt: null, // array of lower case country codes
    enabledVendors: IAB_VENDORS, // array of IAB CMP vendor IDs
    enabledProviders: AC_PROVIDERS.map((provider) => provider.id), // array of AC providers IDs
    language: null,
    queryParamName: null,
    preventScrollOn: 'body',
    track: true,
    zIndex: 9999999,
    onAcceptTracking() {
        debug('MODAL', 'User opted in to tracking');
    },
    onRejectTracking() {
        debug('MODAL', 'User opted out of tracking');
    },
    onConsentsReady() {
        debug('MODAL', 'Consents ready');
    },
};
 
export const DEFAULT_CCPA_OPTIONS = {
    country: null, // country code
    region: null, // region code
    countriesRequiringPrompt: ['us'], // array of lower case country codes
    isSubjectToCcpa: window && window.ads && window.ads.context && window.ads.context.opts
                     && window.ads.context.opts.isSubjectToCcpa,
};
 
function initializeGDPR(options) {
    const depOptions = Object.assign({}, DEFAULT_OPTIONS, options);
    const geoManager = new GeoManager(depOptions.country, depOptions.region);
 
    Eif (!geoManager.hasSpecialPrivacyLaw()) {
        setTimeout(() => {
            depOptions.onAcceptTracking();
            depOptions.onConsentsReady();
        });
        return Promise.resolve({
            getConsent: () => ({
                gdprConsent: true,
                geoRequiresConsent: false,
            })
        });
    }
 
    return import(/* webpackChunkName: "gdpr" */ './gdpr/index.js').then(({createInstance}) => {
        const instance = createInstance(geoManager, depOptions);
        if (!depOptions.oneTrustEnabled) {
            instance.render();
        }
        return instance;
    });
}
 
function initializeCCPA(options) {
    const {
        test,
        ...depOptions
    } = Object.assign({}, DEFAULT_CCPA_OPTIONS, options);
 
    const geoManager = new GeoManager(depOptions.country, depOptions.region, depOptions.countriesRequiringPrompt);
    const userSignalMechanism = new UserSignalMechanism({
        ccpaApplies: geoManager.hasSpecialPrivacyLaw(),
        isSubjectToCcpa: depOptions.isSubjectToCoppa === undefined
                         ? depOptions.isSubjectToCcpa
                         : depOptions.isSubjectToCoppa,
    });
 
    Eif (!depOptions.oneTrustEnabled) {
        userSignalMechanism.install();
    }
 
    return userSignalMechanism;
}
 
function isOneTrustEnabled() {
    const params = new URLSearchParams(window.location.search);
    const ads = (window.ads = window.ads || {});
    const context = (ads.context = ads.context || {});
 
    return JSON.parse(params.get('onetrust_enabled')) || context.oneTrustEnabled || false;
}
 
export default function main(options) {
    const consentsAction = '[AdEngine OptIn] set opt in';
    const instancesAction = '[AdEngine OptIn] set opt in instances';
    const oneTrustEnabled = isOneTrustEnabled();
 
    debug('MODAL', 'Library loaded and started');
 
    Iif (!window.navigator.cookieEnabled) {
        debug('MODAL', 'Cookies are disabled - ignoring CMP and USAPI consent checks');
        communicationService.dispatch({
            type: consentsAction,
            gdprConsent: true,
            geoRequiresConsent: true,
            ccpaSignal: false,
            geoRequiresSignal: true,
        });
 
        return;
    }
 
    const optInInstances = {gdpr: null, ccpa: null};
    const onConsentsReady = () => {
        communicationService.dispatch({
            type: consentsAction,
            ...optInInstances.gdpr.getConsent(),
            ...optInInstances.ccpa.getSignal(),
        });
        communicationService.dispatch({
            type: instancesAction,
            ...optInInstances,
        });
    };
 
    Object.assign(options, {onConsentsReady, oneTrustEnabled});
 
    return initializeGDPR(options).then((gdpr) => {
        optInInstances.gdpr = gdpr;
        optInInstances.ccpa = initializeCCPA(options);
        Iif (oneTrustEnabled) {
            import(/* webpackChunkName: "onetrust" */ './onetrust/index.js').then(({oneTrust}) => {
                oneTrust.initialize(optInInstances, options);
            });
        }
        return optInInstances;
    });
}
 
const autostartModal = () => {
    Eif (!window.trackingOptInManualStart) {
        main(window.trackingOptInOptions || {}).then((optInInstances) => {
            window.trackingOptInInstances = optInInstances;
        });
    }
};
 
ensureGeoCookie().then(() => {
    Eif (document.readyState !== 'loading') {
        autostartModal();
    } else {
        document.addEventListener('DOMContentLoaded', () => {
            autostartModal();
        });
    }
});