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

@web3analytic/notifications-sdk

v1.0.8

Published

Web3Analytic's typescript package for in-dapp notifications

Downloads

11

Readme

notifications-sdk

Web3Analytic's javascript SDK for in-dapp notifications

Install

npm install @web3analytic/notifications-sdk

Usage

The most important function to import is registerImpression which takes as input an address (who may be interacting with your protocol on the frontend), and returns a prompt to show them that was created in the Web3Analytic frontend.

import { registerImpression } from '@web3analytic/notifications-sdk';

You may also want to check out web3analytic-react for a related React component that should be used in tandem with this SDK.

Applications

We provide walkthroughs for the common use cases.

In-Dapp Notifications

Import the following lines:

import { registerImpression } from '@web3analytic/notifications-sdk';
import { DappNotification } from '@web3analytic/notifications-react';

Now, you can render a component conditionally. For instance, here is an example of showing an in-dapp notification when a user connects an addess to MetaMask:

// Saves the ID of an impression
const [impressionId, setImpressionId] = useState(null);
// Saves the prompt returned by the SDK
const [notificationPrompt, setNotificationPrompt] = useState(null);
// Web3Analytic API key
const apiKey = ...

// ...more code...

/**
 * Callback for when an end-user connects their wallet to your dapp
 */
async function OnConnectToMetamask() {
  if (window.ethereum) {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    try {
      const address = await signer.getAddress();
      registerImpression(apiKey, address)
      .then(res => {
        if (res.impressionId) { setImpressionId(res.impressionId); }
        if (res.prompt) { setPrompt(res.prompt); }
      })
      .catch(err => {
        console.log('App `registerImpression` error:', err);
      });
    } catch (error) {
      console.log("web3analytic error", error);
    }
  }
}

// ...more code...

// We use `DappNotifications` from the `web3analytic-react` sdk to render a notification.
return (
  <DappNotification
    apiKey={apiKey}
    impressionId={impressionId}
    prompt={prompt}
  />
);

The impressionId is an identifier for the impression object produced by the SDK function registerImpression. This is used to record a reaction to the notification in DappNotification. The prompt object contains the metadata of what to show in the notification; it is also passed to DappNotification to render. The variable apiKey can be found in the Web3Analytic web application on the campaigns page.

Custom Notifications

Custom notifications are protocols that wish to customize the notifications beyond what we can do in the web application. The SDK provides two end points: (1) to register a custom impression in which the user can select usage, social, and wallet criteria, and (2) to register a reaction (e.g. did the user interact with the notification?). In this case, the SDK expects the user to provide their own frontend components.

Import the following lines:

import { registerCustomImpression, registerReaction } from '@web3analytic/notifications-sdk';

Here is the same example of an end-user connecting MetaMask but now with a custom notification:

// Import your own notification (replace this with your path)
import { myNotification } from './components/MyNotification.jsx';
// Saves the ID of an impression
const [impressionId, setImpressionId] = useState(null);
// Web3Analytic API key
const apiKey = ...

// ...more code...

/**
 * Callback for when an end-user connects their wallet to your dapp
 */
async function OnConnectToMetamask() {
  if (window.ethereum) {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    // This criteria says return all hits (no filters by usage, social, nor wallet)
    const criteria = {usage: null, social: null, wallet: null};
    try {
      const address = await signer.getAddress();
      registerCustomImpression(apiKey, address, criteria);
      .then(res => {
        if (res.impressionId) { setImpressionId(res.impressionId); }
      })
      .catch(err => {
        console.log('App `registerImpression` error:', err);
      });
    } catch (error) {
      console.log("web3analytic error", error);
    }
  }
}

/**
 * Function to handle an end-user reacting to a notification
 */
const handleOnReact = async () => {
  await registerReaction(apiKey, impressionId);
}

// ...more code...

// Use the callback in HTML / React / etc
<button onClick={() => { handleOnReact(); }}>

You can do more granular queries with custom notifications based on on-chain activity, social media activity, or wallet balances. For example, maybe you are interested in the subset of your segment who have done a referral campaign on GMX previously.

const criteria = {
  usage: {
    protocol: 'gmx',
    action: 'claim-referral',
    minCount: 0,
  },
  social: null,
  wallet: null,
};

Or you may be interested in those with at least 1k followers on Twitter:

const criteria = {
  usage: null,
  social: {
    channel: 'twitter',
    minFollowers: 1000,
  },
  wallet: null,
};

Or you may be interested in those with a 1k of usdc in their wallet:

const criteria = {
  usage: null;
  social: null;
  wallet: {
    minBalance: 0,
    tokens: {
      symbol: "usdc",
      minBalance: 1000
    }
  }
};