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

@june-so/analytics-node

v8.0.0

Published

The hassle-free way to integrate analytics into any Node.js application

Downloads

21,172

Readme

June Node SDK

Installation

To install the June SDK in your Node.js project, use the npm package manager:

npm install @june-so/analytics-node --save

Alternatively, use yarn:

yarn add @june-so/analytics-node

Configuration

First, you'll need to import the SDK into your application:

import { Analytics } from '@june-so/analytics-node';

Next, instantiate the client using the write key you've received when you created your June account:

const analytics = new Analytics('YOUR_WRITE_KEY');

Now you can use the analytics instance to track events, identify users, and more.

API Reference

new Analytics(writeKey, [options])

Initializes a new Analytics instance.

  • writeKey (String): Your June project's write key.
  • options (Object, optional): Configuration options. Possible fields:
    • flushAt (Number): The number of messages to enqueue before flushing (default: 20).
    • flushInterval (Number): The duration (in ms) to wait before flushing the queue automatically (default: 10000 ms).
    • enable (Boolean): If set to false, turns off data queuing and sending (default: true).
    • axiosConfig (Object): Additional configuration for Axios.
    • axiosRetryConfig (Object): Additional configuration for Axios Retry.
    • retryCount (Number): Number of times to retry a failed request (default: 3).
    • errorHandler (Function): Custom error handling function.

Example:

const analytics = new Analytics('YOUR_WRITE_KEY', {
  flushAt: 50,
  flushInterval: 30000, // flush every 30 seconds
});

Tracking Events

To track events, you can use the track method:

analytics.track({
  userId: 'USER_ID',
  event: 'Signed In',
  properties: {
    browser: 'chrome',
  },
});

Parameters:

  • userId: (Type: String, Optional) The ID for this user in your database. _Note: At least one of userId or anonymousId must be included in any track call.
  • anonymousId: (Type: String, Optional) An ID associated with the user when you don’t know who they are (for example, the anonymousId generated by analytics.js). Note: You must include at least one of userId or anonymousId in all track calls.
  • event: (Type: String) The name of the event you’re tracking. It is recommended to use human-readable names like 'Song Played' or 'Status Updated'.
  • properties: (Type: Object, Optional) A dictionary of properties for the event. For instance, if the event was 'Product Added', it might have properties like price or product.
  • timestamp: (Type: Date, Optional) A JavaScript date object representing when the track took place. If the track just happened, leave it out and the server’s time will be used. If you’re importing data from the past, make sure to send a timestamp.
  • context: (Type: Object, Optional) A dictionary of extra context to attach to the call. Note: context differs from traits because it is not attributes of the user itself.

Identifying Users & Setting User Traits

To identify users, you can use the identify method:

analytics.identify({
  userId: 'USER_ID',
  traits: {
    email: '[email protected]',
    // Optional
    name: 'Joe Bloggs',
    avatar: 'https://avatar.com/asd809sdhoif9as10nc29.png',
    // Add anything else about the user here
  },
});

Parameters:

  • userId: (Type: String, Optional) The ID for this user in your database. Note: At least one of userId or anonymousId must be included in any identify call.
  • anonymousId: (Type: String, Optional) An ID associated with the user when you don’t know who they are (for example, the anonymousId generated by analytics.js). Note: You must include at least one of userId or anonymousId in all identify calls.
  • traits: (Type: Object, Optional) A dictionary of traits you know about the user. Things like: email, name, or friends.
  • timestamp: (Type: Date, Optional) A JavaScript date object representing when the identify took place. If the identify just happened, leave it out as Segment uses the server’s time. If you’re importing data from the past, make sure to send a timestamp.
  • context: (Type: Object, Optional) A dictionary of extra context to attach to the call. Note: context differs from traits because it is not attributes of the user itself.

Grouping Users

To group users by organization or company, use the group method:

analytics.group({
  userId: 'USER_ID',
  groupId: 'GROUP_ID',
  traits: {
    name: 'Acme Inc',
    // Optional
    avatar: 'https://avatar.com/asd809sdhoif9as10nc29.png',
    // Add anything else about the company here
  },
});

Parameters:

  • userId: (Type: String, Optional) The ID for this user in your database. _Note: At least one of userId or anonymousId must be included in any group call.
  • anonymousId: (Type: String, Optional) An ID associated with the user when you don’t know who they are (e.g., the anonymousId generated by analytics.js). Note: At least one of userId or anonymousId must be included in any group call.
  • groupId: (Type: String) The ID of the group.
  • traits: (Type: Dictionary, Optional) A dictionary of traits you know about the group. For a company, they might be things like name, address, or phone.
  • context: (Type: Dictionary, Optional) A dictionary containing any context about the request. To see the full reference of supported keys, check them out in the context reference.
  • timestamp: (Type: DateTime, Optional) A DateTime object representing when the group took place. If the group just happened, leave it out and we’ll use the server’s time. If you’re importing data from the past, make sure to send a timestamp.

Serverless environments

In serverless environemnts like AWS Lambda, your environment may finish the code execution before June SDK is able to send events to June API.

You can use the flush method to send all queued events to June or you can configure the SDK to flush events automatically.

Flush events manually

analytics.flush();

Flush events automatically

If you set flushAt to 1, the SDK will flush events automatically after every event.

const analytics = new Analytics('YOUR_WRITE_KEY', {
  flushAt: 1,
});