Skip to content

Config-based encryption for analytics on the server and in the browser.

License

Notifications You must be signed in to change notification settings

usermirror/analytics-encryption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


analytics-encryption

Config-based encryption for analytics
on the server and in the browser.



Getting Started

Installation

Install with npm:

npm install --save analytics-encryption

Or with yarn:

yarn add analytics-encryption

Examples

import analyticsEncryption from 'analytics-encryption'

// Tries to find `window.analytics` and extend `.track/identify()`
const encryptedAnalytics = analyticsEncryption()

// Or you can pass in an analytics object
const analytics = analyticsEncryption({ analytics: trackingV1 })

Usage

Node.js

// ./analytics.js
import Analytics from 'analytics-node'
import EncryptionClient from '@ume/client'
import analyticsEncryption from 'analytics-encryption'

const apiHost = process.env.UME_API_HOST
const eclient = new EncryptionClient({ apiHost })
const originalAnalytics = new Analytics(process.env.WRITE_KEY)

const analytics = analyticsEncryption({
  analytics: originalAnalytics,
  local: { encrypt: eclient.encrypt.bind(eclient) }
})

const { track, identify } = analytics

export default analytics
export { track, identify }

// ./app.js
import { track } from './analytics'

track('Form Submitted', {
  email: 'name@example.com',
  name: 'Example Name',
  dob: '01-01-1999'
})

// turns into

originalAnalytics.track('Form Submitted', {
  email: 'r8Udt6Swa+Znk7CI5+9W/0xQ7PckBj3+H983fun8LEAtLFnzvJzeuq',
  name: 'ckBj3+H983ful8KUHuKglyawtfeKLVf4EAN3XEkKhmX',
  dob: 'xt5crfiZhr5M7IPcbb2q8f8uB/Et77q'
})

Browser

// ./init.js
import EncryptionClient from '@ume/client'
import analyticsEncryption from 'analytics-encryption'

const apiHost = process.env.UME_API_HOST
const eclient = new EncryptionClient({ apiHost })

analyticsEncryption({
  replace: true,
  local: { encrypt: eclient.encrypt.bind(eclient) }
})

// ./app.js
window.analytics.track('Form Submitted', {
  email: 'name@example.com',
  name: 'Example Name',
  dob: '01-01-1999'
})

// turns into

window.analytics.original.track('Form Submitted', {
  email: 'r8Udt6Swa+Znk7CI5+9W/0xQ7PckBj3+H983fun8LEAtLFnzvJzeuq',
  name: 'ckBj3+H983ful8KUHuKglyawtfeKLVf4EAN3XEkKhmX',
  dob: 'xt5crfiZhr5M7IPcbb2q8f8uB/Et77q'
})

Contributing

All contributions are welcome, analytics-encryption is MIT-licensed.

About

Config-based encryption for analytics on the server and in the browser.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published