|
1 | | -export default (name: string) => `Hello, ${name} !` |
| 1 | +import { encryptString, parsePublicKey } from '@chiffre/crypto-box' |
| 2 | +import { |
| 3 | + Event, |
| 4 | + setupSessionListeners, |
| 5 | + setupPageVisitListeners, |
| 6 | + createGenericEvent, |
| 7 | + createBrowserEvent |
| 8 | +} from '@chiffre/analytics-core' |
| 9 | +import { Config } from './types' |
| 10 | + |
| 11 | +export function readConfig(): Config | null { |
| 12 | + try { |
| 13 | + const config = JSON.parse( |
| 14 | + document.getElementById('chiffre:analytics-config')?.innerText || '{}' |
| 15 | + ) |
| 16 | + if (!config.pushURL) { |
| 17 | + throw new Error('Missing pushURL') |
| 18 | + } |
| 19 | + return { |
| 20 | + publicKey: parsePublicKey(config.publicKey), |
| 21 | + pushURL: config.pushURL |
| 22 | + } |
| 23 | + } catch (error) { |
| 24 | + console.error( |
| 25 | + '[Chiffre] Failed to load Chiffre analytics configuration:', |
| 26 | + error |
| 27 | + ) |
| 28 | + return null |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export function sendEvent(event: Event<any, any>, config: Config) { |
| 33 | + const tick = performance.now() |
| 34 | + const json = JSON.stringify(event) |
| 35 | + const payload = encryptString(json, config.publicKey) |
| 36 | + const tock = performance.now() |
| 37 | + const perf = Math.round(tock - tick) |
| 38 | + const url = `${config.pushURL}?perf=${perf}` |
| 39 | + if (window.localStorage.getItem('chiffre:debug') === 'true') { |
| 40 | + console.dir({ |
| 41 | + event, |
| 42 | + payload, |
| 43 | + perf, |
| 44 | + url |
| 45 | + }) |
| 46 | + } |
| 47 | + if (window.localStorage.getItem('chiffre:no-send') === 'true') { |
| 48 | + console.info('[Chiffre] Skip sending message (chiffre:no-send is set)', { |
| 49 | + payload, |
| 50 | + perf |
| 51 | + }) |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + // Try sendBeacon first, if available |
| 56 | + if ( |
| 57 | + typeof navigator.sendBeacon === 'function' && |
| 58 | + navigator.sendBeacon(url, payload) |
| 59 | + ) { |
| 60 | + return true |
| 61 | + } |
| 62 | + |
| 63 | + // Fallback to img GET |
| 64 | + const img = new Image() |
| 65 | + img.src = `${url}&payload=${payload}` |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +export function setup() { |
| 70 | + window.chiffre = { |
| 71 | + sendNumber: () => {}, |
| 72 | + sendNumbers: () => {}, |
| 73 | + sendString: () => {}, |
| 74 | + sendStrings: () => {} |
| 75 | + } |
| 76 | + const config = readConfig() |
| 77 | + if (!config) { |
| 78 | + return |
| 79 | + } |
| 80 | + if (navigator.doNotTrack === '1') { |
| 81 | + // With DoNotTrack, we send a single event for page views, without |
| 82 | + // any session tracking or other visitor information. |
| 83 | + sendEvent(createBrowserEvent('session:dnt'), config) |
| 84 | + } else { |
| 85 | + setupSessionListeners(event => sendEvent(event, config)) |
| 86 | + setupPageVisitListeners(event => sendEvent(event, config)) |
| 87 | + } |
| 88 | + window.chiffre.sendNumber = data => { |
| 89 | + const event = createGenericEvent('generic:number', data) |
| 90 | + sendEvent(event, config) |
| 91 | + } |
| 92 | + window.chiffre.sendNumbers = data => { |
| 93 | + const event = createGenericEvent('generic:numbers', data) |
| 94 | + sendEvent(event, config) |
| 95 | + } |
| 96 | + window.chiffre.sendString = data => { |
| 97 | + const event = createGenericEvent('generic:string', data) |
| 98 | + sendEvent(event, config) |
| 99 | + } |
| 100 | + window.chiffre.sendStrings = data => { |
| 101 | + const event = createGenericEvent('generic:strings', data) |
| 102 | + sendEvent(event, config) |
| 103 | + } |
| 104 | +} |
0 commit comments