|
| 1 | +import {loadScript, validateData} from '#3p/3p'; |
| 2 | + |
| 3 | +import {setStyles} from '#core/dom/style'; |
| 4 | + |
| 5 | +import {dev, user} from '#utils/log'; |
| 6 | + |
| 7 | +const CLICKIO_LOG_TAG = 'Clickio'; |
| 8 | +const CLICKIO_GLOBAL_NAME = '__lxG__'; |
| 9 | +const CLICKIO_GLOBAL_COMMON_NAME = '__lxGc__'; |
| 10 | +/** |
| 11 | + * @param {!Window} global |
| 12 | + * @param {!Object} data |
| 13 | + */ |
| 14 | +export function clickio(global, data) { |
| 15 | + if (data.siteId && data.unitId) { |
| 16 | + validateData(data, ['siteId', 'unitId']); |
| 17 | + |
| 18 | + const divContainer = global.document.getElementById('c'); |
| 19 | + const div = global.document.createElement('div'); |
| 20 | + |
| 21 | + div.setAttribute('id', Math.round(Math.random() * 1e8).toString(36)); |
| 22 | + |
| 23 | + if (divContainer) { |
| 24 | + // clickio global |
| 25 | + global[CLICKIO_GLOBAL_NAME] = global[CLICKIO_GLOBAL_NAME] || {}; |
| 26 | + |
| 27 | + const clickioGlobal = global[CLICKIO_GLOBAL_NAME]; |
| 28 | + |
| 29 | + // base config |
| 30 | + clickioGlobal.ampMode = context.isMaster ? 1 : 2; |
| 31 | + clickioGlobal.pageUrl = global.context.location.href; |
| 32 | + clickioGlobal.sendPageUrl = true; |
| 33 | + |
| 34 | + // ad container |
| 35 | + divContainer.appendChild(div); |
| 36 | + setStyles(divContainer, { |
| 37 | + top: '50%', |
| 38 | + left: '50%', |
| 39 | + bottom: '', |
| 40 | + right: '', |
| 41 | + transform: 'translate(-50%, -50%)', |
| 42 | + }); |
| 43 | + |
| 44 | + // ad code |
| 45 | + (global[CLICKIO_GLOBAL_COMMON_NAME] = global[ |
| 46 | + CLICKIO_GLOBAL_COMMON_NAME |
| 47 | + ] || {'s': {}, 'b': 0}).cmd = |
| 48 | + global[CLICKIO_GLOBAL_COMMON_NAME].cmd || []; |
| 49 | + global[CLICKIO_GLOBAL_COMMON_NAME].cmd.push(function () { |
| 50 | + global[CLICKIO_GLOBAL_COMMON_NAME].display( |
| 51 | + div.id, |
| 52 | + '_' + data.siteId, |
| 53 | + '_' + data.unitId, |
| 54 | + data |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + // load clickio script |
| 59 | + loadScript( |
| 60 | + global, |
| 61 | + 'https://s.clickiocdn.com/t/' + data.siteId + '/360_amp.js' |
| 62 | + ); |
| 63 | + |
| 64 | + // load consent module for master |
| 65 | + if (context.isMaster) { |
| 66 | + // consent module |
| 67 | + if ( |
| 68 | + context.initialConsentMetadata !== null && |
| 69 | + typeof context.initialConsentMetadata === 'object' && |
| 70 | + context.initialConsentMetadata.gdprApplies && |
| 71 | + context.initialConsentValue !== '' |
| 72 | + ) { |
| 73 | + clickioGlobal.consent = { |
| 74 | + gdpr: context.initialConsentMetadata.gdprApplies, |
| 75 | + consentString: context.initialConsentValue, |
| 76 | + additionalConsent: context.initialConsentMetadata.additionalConsent, |
| 77 | + }; |
| 78 | + |
| 79 | + // load consent script |
| 80 | + loadScript( |
| 81 | + global, |
| 82 | + 'https://s.clickiocdn.com/t/static/amp/consent_amp.js' |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + dev().info( |
| 88 | + CLICKIO_LOG_TAG, |
| 89 | + 'clickio: running (' + (context.isMaster ? 'master' : 'slave') + ')' |
| 90 | + ); |
| 91 | + } |
| 92 | + } else { |
| 93 | + user().error(CLICKIO_LOG_TAG, 'clickio: siteId and unitId are required'); |
| 94 | + return; |
| 95 | + } |
| 96 | +} |
0 commit comments