Skip to content

Commit 1b01191

Browse files
committed
feat: Initial commit
1 parent 94b5978 commit 1b01191

File tree

11 files changed

+3418
-131
lines changed

11 files changed

+3418
-131
lines changed

.github/workflows/cd.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- run: yarn install --ignore-scripts
2828
name: Install dependencies
2929
- run: yarn build
30-
name: Build package
30+
name: Build bundle
3131

3232
# Continuous Delivery Pipeline --
3333

@@ -37,3 +37,11 @@ jobs:
3737
env:
3838
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3939
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
40+
- uses: dswistowski/surge-sh-action@341bcbd
41+
name: Deploy to Surge
42+
if: steps.semantic.outputs.new-release-published == 'true'
43+
with:
44+
domain: https://embed.chiffre.io
45+
project: ./dist
46+
login: ${{ secrets.SURGE_LOGIN }}
47+
token: ${{ secrets.SURGE_TOKEN }}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
node_modules/
22
dist/
3+
lib/
34
coverage/
45
.env
56
yarn-error.log
6-
# Docker containers with persistance
7-
.volumes/
7+
.cache/

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ tsconfig.json
55
yarn-error.log
66
.github/**
77
src/**
8+
.cache/
9+
dist/
10+
lib/main.*
11+
.dependabot/
12+
scripts/

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# @chiffre/template-library
1+
# @chiffre/analytics-tracker
22

3-
[![NPM](https://img.shields.io/npm/v/@chiffre/template-library?color=red)](https://www.npmjs.com/package/@chiffre/template-library)
4-
[![MIT License](https://img.shields.io/github/license/chiffre-io/template-library.svg?color=blue)](https://github.com/chiffre-io/template-library/blob/next/LICENSE)
5-
[![Continuous Integration](https://github.com/chiffre-io/template-library/workflows/Continuous%20Integration/badge.svg?branch=next)](https://github.com/chiffre-io/template-library/actions)
6-
[![Coverage Status](https://coveralls.io/repos/github/chiffre-io/template-library/badge.svg?branch=next)](https://coveralls.io/github/chiffre-io/template-library?branch=next)
7-
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=chiffre-io/template-library)](https://dependabot.com)
3+
[![NPM](https://img.shields.io/npm/v/@chiffre/analytics-tracker?color=red)](https://www.npmjs.com/package/@chiffre/analytics-tracker)
4+
[![MIT License](https://img.shields.io/github/license/chiffre-io/analytics-tracker.svg?color=blue)](https://github.com/chiffre-io/analytics-tracker/blob/next/LICENSE)
5+
[![Continuous Integration](https://github.com/chiffre-io/analytics-tracker/workflows/Continuous%20Integration/badge.svg?branch=next)](https://github.com/chiffre-io/analytics-tracker/actions)
6+
[![Coverage Status](https://coveralls.io/repos/github/chiffre-io/analytics-tracker/badge.svg?branch=next)](https://coveralls.io/github/chiffre-io/analytics-tracker?branch=next)
7+
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=chiffre-io/analytics-tracker)](https://dependabot.com)
88

9-
Template for Chiffre libraries
9+
Tracker script for Chiffre Analytics
1010

1111
## License
1212

13-
[MIT](https://github.com/chiffre-io/template-library/blob/next/LICENSE) - Made with ❤️ by [François Best](https://francoisbest.com).
13+
[MIT](https://github.com/chiffre-io/analytics-tracker/blob/next/LICENSE) - Made with ❤️ by [François Best](https://francoisbest.com).

package.json

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "@chiffre/template-library",
2+
"name": "@chiffre/analytics-tracker",
33
"version": "0.0.0-semantically-released",
4-
"description": "Template for Chiffre libraries",
5-
"main": "dist/index.js",
4+
"description": "Tracker script for Chiffre Analytics",
5+
"main": "lib/index.js",
66
"license": "MIT",
77
"author": {
88
"name": "François Best",
@@ -11,24 +11,29 @@
1111
},
1212
"repository": {
1313
"type": "git",
14-
"url": "https://github.com/chiffre-io/template-library"
14+
"url": "https://github.com/chiffre-io/analytics-tracker"
1515
},
1616
"keywords": [
1717
"chiffre",
18-
"template"
18+
"analytics"
1919
],
2020
"publishConfig": {
2121
"access": "public"
2222
},
2323
"scripts": {
2424
"test": "jest --coverage",
2525
"test:watch": "jest --watch",
26-
"build:clean": "rm -rf ./dist",
26+
"build:clean": "rm -rf ./dist ./lib ./coverage",
2727
"build:ts": "tsc",
28-
"build": "run-s build:clean build:ts",
29-
"ci": "run-s build test"
28+
"build:bundle": "parcel build ./src/main.ts --out-file analytics.js --detailed-report 20 --experimental-scope-hoisting",
29+
"build:cors": "echo '*' > ./dist/CORS",
30+
"build": "run-s build:clean build:ts build:bundle build:cors",
31+
"ci": "run-s build"
32+
},
33+
"dependencies": {
34+
"@chiffre/analytics-core": "^1.0.0",
35+
"@chiffre/crypto-box": "^1.0.1"
3036
},
31-
"dependencies": {},
3237
"devDependencies": {
3338
"@commitlint/config-conventional": "^8.3.4",
3439
"@types/jest": "^25.1.4",
@@ -37,14 +42,19 @@
3742
"husky": "^4.2.3",
3843
"jest": "^25.1.0",
3944
"npm-run-all": "^4.1.5",
45+
"parcel-bundler": "^1.12.4",
4046
"ts-jest": "^25.2.1",
4147
"ts-node": "^8.6.2",
4248
"typescript": "^3.8.3"
4349
},
50+
"browser": {
51+
"crypto": false,
52+
"buffer": false
53+
},
4454
"jest": {
4555
"verbose": true,
4656
"preset": "ts-jest/presets/js-with-ts",
47-
"testEnvironment": "node"
57+
"testEnvironment": "jsdom"
4858
},
4959
"husky": {
5060
"hooks": {

src/index.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.ts

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,104 @@
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+
}

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { setup } from './index'
2+
3+
setup()

src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { GenericDataPoint } from '@chiffre/analytics-core'
2+
3+
export interface Config {
4+
publicKey: Uint8Array
5+
pushURL: string
6+
}
7+
8+
declare global {
9+
interface Window {
10+
chiffre: {
11+
// Generic events
12+
sendNumber: (data: GenericDataPoint<number>) => void
13+
sendNumbers: (data: GenericDataPoint<number>[]) => void
14+
sendString: (data: GenericDataPoint<string>) => void
15+
sendStrings: (data: GenericDataPoint<string>[]) => void
16+
}
17+
}
18+
}

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"lib": ["DOM"],
66
"module": "commonjs",
77
"declaration": true,
8-
"outDir": "./dist",
8+
"outDir": "./lib",
99
"rootDir": "./src",
1010
"esModuleInterop": true,
1111

@@ -29,5 +29,5 @@
2929
"emitDecoratorMetadata": true
3030
},
3131
"include": ["./src/**/*.ts"],
32-
"exclude": ["./src/**/*.test.ts", "./dist", "./node_modules"]
32+
"exclude": ["./src/**/*.test.ts", "./dist", "./node_modules", "./src/main.ts"]
3333
}

0 commit comments

Comments
 (0)