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

pirsch-sdk

v2.7.0

Published

TypeScript/JavaScript client SDK for Pirsch.

Downloads

1,768

Readme

Pirsch JavaScript SDK

This is the official JavaScript client SDK for Pirsch. For details, please check out our documentation.

Installation

npm i pirsch-sdk

Usage

Configuration

The SDK is configured using the constructor. We recommend using an access key instead of a client ID + secret if you only need write access (sending page views and events), as it saves a roundtrip to the server when refreshing the access token.

If you run your server-side integration behind a proxy or load balancer, make sure you correctly configure trustedProxyHeaders. They will be used to extract the real visitor IP for each request. They will be used in the order they are passed into the configuration. Possible values are: "cf-connecting-ip", "x-forwarded-for", "forwarded", "x-real-ip".

Server-Side

Here is a quick demo on how to use this library in NodeJS:

import { createServer } from "node:http";
import { URL } from "node:url";

// Import the Pirsch client.
import { Pirsch } from "pirsch-sdk";

// Create a client with the hostname, client ID, and client secret you have configured on the Pirsch dashboard.
const client = new Pirsch({
    hostname: "example.com",
    protocol: "http", // used to parse the request URL, default is https
    clientId: "<client_id>",
    clientSecret: "<client_secret or access_key>"
});

// Create your http handler and start the server.
createServer((request, response) => {
    // In this example, we only want to track the / path and nothing else.
    // We parse the request URL to read and check the pathname.
    const url = new URL(request.url || "", "http://localhost:8765");

    if (url.pathname === "/") {
        // Send the hit to Pirsch. hitFromRequest is a helper function that returns all required information from the request.
        // You can also built the Hit object on your own and pass it in.
        client.hit(client.hitFromRequest(request)).catch(error => {
            // Something went wrong, check the error output.
            console.error(error);
        });
    }

    // Render your website...
    response.write("Hello from Pirsch!");
    response.end();
}).listen(8765);

Client-Side

Here is how you can do the same in the browser:

// Import the Pirsch client.
import { Pirsch } from "pirsch-sdk/web";

// Create a client with the identification code you have configured on the Pirsch dashboard.
const client = new Pirsch({
    identificationCode: "<identification_code>"
});

const main = async () => {
    await client.hit();

    await client.event("test-event", 60, { clicks: 1, test: "xyz" });
}

void main();

FAQ

This module export three Clients (pirsch-sdk, pirsch-sdk/web-api and pirsch-sdk/web), what are the differences?

  • pirsch-sdk and pirsch-sdk/web-api are based on the same core logic, and function the same. It can be used to access and sending data via the API. pirsch-sdk/web-api is a version of the Node client that works in the web. You will rarely need to use this version though.
  • pirsch-sdk/web is a modular version of the JS Snippet, that has no automatic functionality. You need to send any hits or events yourself.

:information_source: Basically your choice will be between pirsch-sdk (Node, backend, accessing or sending data) or pirsch-sdk/web, (Browser, frontend, sending data) in 99% of the cases.

Changelog

See CHANGELOG.md.

License

MIT