npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ht-sdks/events-sdk-js-browser

v1.3.0

Published

## Installation via CDN

Downloads

6,215

Readme

Events Javascript SDK

Installation via CDN

To integrate the JavaScript SDK with your website, place the following code snippet in the <head> section of your website.

<script type="text/javascript">
!function(){var e=window.htevents=window.htevents||[];if(!e.initialize)if(e.invoked)window.console&&console.error&&console.error("Hightouch snippet included twice.");else{e.invoked=!0,e.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"],e.factory=function(t){return function(){var n=Array.prototype.slice.call(arguments);return n.unshift(t),e.push(n),e}};for(var t=0;t<e.methods.length;t++){var n=e.methods[t];e[n]=e.factory(n)}e.load=function(t,n){var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src="https://cdn.hightouch-events.com/browser/release/v1-latest/events.min.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(o,r),e._loadOptions=n,e._writeKey=t},e.SNIPPET_VERSION="0.0.1",
e.load(<WRITE_KEY>,{apiHost:<DATA_PLANE_URL>}),
e.page()}}();
</script>

window.htevents.track(...) will then be available for use.

Alternative installation using NPM

  1. Install the package
# npm
npm install @ht-sdks/events-sdk-js-browser

# yarn
yarn add @ht-sdks/events-sdk-js-browser

# pnpm
pnpm add @ht-sdks/events-sdk-js-browser
  1. Import the package into your project and you're good to go (with working types)!
import { HtEventsBrowser } from '@ht-sdks/events-sdk-js-browser'

const htevents = HtEventsBrowser.load({ writeKey: '<YOUR_WRITE_KEY>' })

htevents.identify('hello world')

document.body?.addEventListener('click', () => {
  htevents.track('document body clicked!')
})

Lazy / Delayed Loading

You can load a buffered version of htevents that requires .load to be explicitly called before initiating any network activity. This can be useful if you want to wait for a user to consent before fetching any tracking destinations or sending buffered events to hightouch.

  • ⚠️ ️.load should only be called once.
export const htevents = new HtEventsBrowser()

htevents.identify("hello world")

if (userConsentsToBeingTracked) {
    htevents.load({ writeKey: '<YOUR_WRITE_KEY>' }) // destinations loaded, enqueued events are flushed
}

Error Handling

Handling initialization errors

If you want to catch initialization errors, you can do the following:

export const htevents = new HtEventsBrowser();
htevents
  .load({ writeKey: "MY_WRITE_KEY" })
  .catch((err) => ...);

Usage in Common Frameworks / SPAs

Vanilla React

import { HtEventsBrowser } from '@ht-sdks/events-sdk-js-browser'

// We can export this instance to share with rest of our codebase.
export const htevents = HtEventsBrowser.load({ writeKey: '<YOUR_WRITE_KEY>' })

const App = () => (
  <div>
    <button onClick={() => htevents.track('hello world')}>Track</button>
  </div>
)

Vue

  1. Export htevents instance. E.g. services/hightouch.ts
import { HtEventsBrowser } from '@ht-sdks/events-sdk-js-browser'

export const htevents = HtEventsBrowser.load({
  writeKey: '<YOUR_WRITE_KEY>',
})
  1. in .vue component
<template>
  <button @click="track()">Track</button>
</template>

<script>
import { defineComponent } from 'vue'
import { htevents } from './services/hightouch'

export default defineComponent({
  setup() {
    function track() {
      htevents.track('Hello world')
    }

    return {
      track,
    }
  },
})
</script>

How to add typescript support when using the CDN snippet

NOTE: this is only required for snippet installation.

NPM installation should already have type support.

  1. Install npm package @ht-sdks/events-sdk-js-browser as a dev dependency.

  2. Create ./typings/htevents.d.ts

// ./typings/htevents.d.ts
import type { HtEventsSnippet } from "@ht-sdks/events-sdk-js-browser";

declare global {
  interface Window {
    htevents: HtEventsSnippet;
  }
}
  1. Configure typescript to read from the custom ./typings folder
// tsconfig.json
{
  ...
  "compilerOptions": {
    ....
    "typeRoots": [
      "./node_modules/@types",
      "./typings"
    ]
  }
  ....
}

Development

First, clone the repo and then startup our local dev environment:

$ git clone [email protected]:ht-sdks/events-sdk-js-mono.git
$ cd events-sdk-js-mono
$ nvm use  # installs correct version of node defined in .nvmrc.
$ npm install
$ npx turbo run build
$ npx turbo run test

If you get "Cannot find module '@ht-sdks/events-sdk-js-browser' or its corresponding type declarations.ts(2307)" (in VSCode), you may have to "cmd+shift+p -> "TypeScript: Restart TS server"

Plugins

When developing against Events SDK JS you will likely be writing plugins, which can augment functionality and enrich data. Plugins are isolated chunks which you can build, test, version, and deploy independently of the rest of the codebase. Plugins are bounded by Events SDK JS which handles things such as observability, retries, and error management.

Plugins can be of two different priorities:

  1. Critical: Events SDK JS should expect this plugin to be loaded before starting event delivery
  2. Non-critical: Events SDK JS can start event delivery before this plugin has finished loading

and can be of five different types:

  1. Before: Plugins that need to be run before any other plugins are run. An example of this would be validating events before passing them along to other plugins.
  2. After: Plugins that need to run after all other plugins have run. An example of this is the Hightouch.io integration, which will wait for destinations to succeed or fail so that it can send its observability metrics.
  3. Destination: Destinations to send the event to (ie. legacy destinations). Does not modify the event and failure does not halt execution.
  4. Enrichment: Modifies an event, failure here could halt the event pipeline.
  5. Utility: Plugins that change Events SDK JS functionality and don't fall into the other categories.

Here is an example of a simple plugin that would convert all track events event names to lowercase before the event gets sent through the rest of the pipeline:

import type { Plugin } from '@ht-sdks/events-sdk-js-browser'

export const lowercase: Plugin = {
  name: 'Lowercase Event Name',
  type: 'before',
  version: '1.0.0',

  isLoaded: () => true,
  load: () => Promise.resolve(),

  track: (ctx) => {
    ctx.event.event = ctx.event.event.toLowerCase()
    return ctx
  }
}

htevents.register(lowercase)

For further examples check out our existing plugins.

Client-side destinations

The Browser SDK supports sending events directly from the client to destinations which is useful in situations where the destination requires a client-side context in order to fully enrich and attribute events.

Google Analytics 4

Google Analytics 4 (GA4) offers tracking via Google Tag Manager (GTM) which may benefit from a client-side integration.

Installation

Make sure your GA4 setup scripts are configured on your website. Our implementation expects the gtag function to be available in the global scope.

<!-- example GA4 setup using Google Tag Manager -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXXX');
</script>

You can then configure the Browser SDK to send events directly to GA4 by enabling the Google Tag Manager destination:

htevents.load("WRITE_KEY", {
  destinations: {
    "Google Tag Manager": {
      measurementId: "G-XXXXXXXX"
    }
  }
})

View the complete plugin documentation in google-tag-manager.ts

Usage

Once the destination is configured, all applicable identify, track, and page events will be sent. The integration also automatically populates the user_id and hightouch_anonymous_id fields.

htevents.track('My Event', { prop: 'abc' })
// gtag('event', 'My Event', { prop: 'abc', user_id: '123' })

Custom client-side destinations

If you'd like to send events to a custom client-side destination that is not yet supported, you can do so using the Destination class as a template and implement the relevant tracking methods (track, page, etc).

import { HtEventsBrowser, Destination } from "@ht-sdks/events-sdk-js-browser";

const htevents = new HtEventsBrowser();

htevents.load({ writeKey: "WRITE_KEY" });

// register custom client-side destination
htevents.register(
  new Destination("Console", "1.2.3", {
    track: (ctx) => {
      console.log("[console.track]", ctx.event);
    },
  })
);

QA

Feature work and bug fixes should include tests. Run all Jest tests:

$ npx turbo test

Lint all with ESLint:

$ npx turbo lint