Typesafe Analytics

Improve our analytics code using Typescript

Clean data is important!

You can leverage typescript to ensure that events and payloads are correct.

Patterns for Type Safety

See the full source code for this video on github

Jack is the man! Make sure to subscribe to his youtube channel!

Runtime validation

It's also possible to verify the analytics at runtime by adding a custom plugin or using something like the prebaked event-validation plugin

Here's an example of a custom plugin to verify events before tracking data is sent to providers.

import Analytics from 'analytics'

function eventValidationPlugin(pluginConfig = {}) {
  return {
    name: 'my-custom-event-validation-plugins',
    trackStart: ({ payload, config, abort }) => {
      const { event, properties } = payload
      if (event !== 'foo') {
        throw new Error('Oh no this data is wrong. Please fix developer!')

const analytics = Analytics({
  app: 'app-name',
  plugins: [
    // Add plugin before provider integrations
    // ... your analytics integrations like google analytics etc