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

@candecide/client-nodejs

v4.1.3

Published

A client library for the CanDecide API

Downloads

249

Readme

CanDecide Node.js SDK

The CanDecide Node.js SDK allows developers to integrate the capabilities of the CanDecide platform into their Node.js applications seamlessly. This SDK provides functionalities to track user events, log data, manage user information, and handle session data efficiently with a focus on performance and simplicity.

Features

  • Event Tracking: Track and buffer user events and send them in bulk to optimize network usage.
  • Metric Tracking: Track and buffer metrics data to identify values over time in your Dashboard
  • User Management: Upsert user data and manage user identifiers.
  • Logging: Support for info, warn, error, and debug logs.
  • Flexible Configuration: Configure project keys, environment, and other parameters.
  • Automated Buffer Management: Events and logs are buffered and sent periodically to reduce network load.
  • Error Handling: Robust error handling mechanisms to ensure stability.

Installation

Install the SDK using npm:

npm install @candecide/client-nodejs

Or using yarn:

yarn add @candecide/client-nodejs

Usage

Initializing the SDK

First, import and initialize the SDK with your project configuration:

import { CanDecideClient } from '@candecide/client-nodejs';

const sdk = new CanDecideClient({
    projectKey: 'your-project-key',
    environmentKey: 'your-environment-key',
    sourceKey: 'your-source-key',
    logEnabled: true,
    trackingEnabled: true,
});

Setting User ID

To track user-specific data, set the user ID:

sdk.setUserId('unique-user-id');

Setting User Data

To track user specific properties

client.setUserData({
    userId: 'user-id',
    userProperties: {
        key: 'value',
    },
});

Enabling and Disabling Tracking and Logging

To enable or disable tracking, use the setTrackingEnabled method:

client.setTrackingEnabled(true); // Enable tracking
client.setTrackingEnabled(false); // Disable tracking
client.setLogsEnabled(true); // Enable logging
client.setLogsEnabled(false); // Disable logging

Setting User ID

To track user-specific data, set the user ID:

sdk.logPageView('https://example.com', {
    additional: 'parameters',
});

Tracking Events

Track an event with optional parameters:

sdk.trackEvent({
    event_name: 'purchase',
    event_parameters: { item: 'book', amount: 19.99 },
    user_id: 'unique-user-id' // This is optional if setUserId has been used
});

Tracking Metrics

Track a metric with a value and timestamp

sdk.trackMetric({
    metric_name: 'total_users',
    metric_value: 50000,
    metric_timestamp: '2021-09-01T12:00:00.000Z'
});

Creating Leads

Create leads directly from the sdk. Name is a required field. Everything else is optional.

sdk.createLead({
  name: "John Doe",
  email: "[email protected]",
  phone: "+1234567890",
  companyName: "Doe Industries",
  industry: "Manufacturing",
  jobTitle: "CEO",
  website: "https://doeindustries.com",
  country: "USA",
  city: "New York",
  state: "NY",
  address: "1234 Broadway Ave",
  postalCode: "10001",
  latitude: 40.7128,
  longitude: -74.0060,
  projectSourceId: "source123",
  projectLeadStatusId: "status123",
  value: 100000,
  ownerId: "owner123"
});

Logging

Log information, warnings, errors, or debug information:

sdk.logger.info('User logged in', { username: 'example_user' });
sdk.logger.warn('Usage limit nearing', { threshold: 90 });
sdk.logger.error('Payment failed', { error_code: 'PAY_001' });
sdk.logger.debug('Debugging data', { debug_info: 'Here' });

Flushing Data Manually

Force a flush of the event and log buffers:

await sdk.forceFlush();

Configuration Options

You can configure the SDK with the following options:

  • projectKey (string): Your project key.
  • environmentKey (string): The environment key (e.g., 'production', 'development').
  • sourceKey (string): A unique identifier for the data source.
  • logEnabled (boolean, optional): Enable or disable logging capabilities.
  • trackingEnabled (boolean, optional): Enable or disable event tracking.
  • baseUrl (string, optional): Base URL for the CanDecide ingestion API.

Best Practices

  • Always handle potential exceptions when calling SDK methods, especially in production environments.

Support

For detailed documentation, visit our official documentation. For further assistance, contact our support team at [email protected].

License

This SDK is released under the MIT license. See the LICENSE file for more details.