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

@dreamdata/analytics-next

v2.0.4

Published

Analytics Next (aka Analytics 2.0) is the latest version of Dreamdata's JavaScript SDK.

Downloads

754

Readme

@dreamdata/analytics-next

Analytics Next (aka Analytics 2.0) is the latest version of Dreamdata's JavaScript SDK.

Quickstart

The easiest and quickest way to get started with Analytics 2.0 is to use it through Dreamdata. Alternatively, you can install it through NPM and do the instrumentation yourself.

Using with Dreamdata

Navigate to Dreamdata and choose JavaScript v2.0. Click "Reconfigure script" to go through a wizard on how to set up the script.

Using as an npm package

  1. Install the package
# npm
npm install @dreamdata/analytics-next

# yarn
yarn add @dreamdata/analytics-next

# pnpm
pnpm add @dreamdata/analytics-next
  1. Import the package into your project and you're good to go (with working types)!
import { AnalyticsBrowser } from '@dreamdata/analytics-next'

const analytics = AnalyticsBrowser.load({ writeKey: '<YOUR_WRITE_KEY>' })

analytics.identify('hello world')

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

Note that you can find your write key on the sources pages in the Dreamdata app.

Lazy / Delayed Loading

You can load a buffered version of analytics 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 sending any tracking data.

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

analytics.identify('hello world')

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

This strategy also comes in handy if you have some settings that are fetched asynchronously.

const analytics = new AnalyticsBrowser()
fetchWriteKey().then((writeKey) => analytics.load({ writeKey }))

analytics.identify('hello world')

Usage in Common Frameworks and SPAs

See or documentation on integrating with SPAs here.

FAQ

Adding Typescript support when using script tag

  1. Install npm package @dreamdata/analytics-next as a dev dependency.

  2. Create ./typings/analytics.d.ts

// ./typings/analytics.d.ts
import type { AnalyticsSnippet } from '@dreamdata/analytics-next'

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

Handling initialization errors

Initialization errors get logged by default, but if you also want to catch these errors, you can do the following:

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