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

@sajari/sdk-js

v2.10.8

Published

Search.io JavaScript SDK

Downloads

25,330

Readme

Search.io Javascript SDK

npm (scoped) Netlify Status build size license

This SDK is a lightweight JavaScript client for querying the Search.io API.

Checkout our React SDK, for a complete set of customisable UI components, and more.

You can also quickly generate search interfaces from the Search.io admin console.

Table of Contents

Install

NPM/Yarn

npm install --save @sajari/sdk-js@next

# or with yarn
yarn add @sajari/sdk-js@next

Usage within your application:

import { Client, SearchIOAnalytics, etc... } from "@sajari/sdk-js";

const client = new Client("<account_id>", "<collection_id>");

Browser

Note that when using the SDK via a <script> tag in a browser, all components will live under window.SajariSDK:

<script src="https://unpkg.com/@sajari/sdk-js@^2/dist/sajarisdk.umd.production.min.js"></script>
<script>
  const client = new SajariSDK.Client("<account_id>", "<collection_id>");
</script>

Getting started

Create a Client for interacting with our API, and then initialise a pipeline to be used for searching. The pipeline determines how the ranking is performed when performing a search.

If you don't have a Search.io account you can sign up.

const pipeline = new Client("<account_id>", "<collection_id>").pipeline("app");

Create a SearchIOAnalytics instance to track events against the query id that produced a search result.

const searchio = new SearchIOAnalytics("<account_id>", "<collection_id>");

Perform a search on the specified pipeline and handle the results. Here we're searching our collection using the app pipeline and updating the analytics instance with the current query id. The field value you specify must be the name of a field in your schema with the Unique constraint.

const values = { q: "puppies" };
pipeline
  .search(values, { type: "EVENT", field: "id" })
  .then(([response, values]) => {
    searchio.updateQueryId(response.queryId);
    // Handle response...
  })
  .catch((error) => {
    // Handle error...
  });

Handling results

Now we're going to add a basic rendering of the results to the page with integrated event tracking. This will persist the tracking event against the current query id in localStorage and send the event data to Search.io to track how users use search results and/or move through an ecommerce purchase funnel.

const values = { q: "puppies" };
pipeline
  .search(values, { type: "EVENT", field: "id" })
  .then(([response, values]) => {
    searchio.updateQueryId(response.queryId);
    response.results.forEach((r) => {
      const item = document.createElement("a");
      item.textContent = r.values.name;
      item.href = r.values.url;
      item.onclick = () => {
        searchio.track("click", r.id);
      };

      document.body.appendChild(item);
    });
  })
  .catch((error) => {
    // Handle error...
  });

Tracking additional events

When a user moves further through your purchase funnel (e.g. adding an item to their shopping cart, completing a purchase, etc) you'll want to track that also, as it will provide feedback to the ranking system. The correct query id will be preserved throughout the ecommerce funnel as long as an event with type click was already tracked earlier.

document.querySelector(".add-to-cart").addEventListener("click", (e) => {
  searchio.track("add_to_cart", e.target.id);
});

document.querySelector(".complete-purchase").addEventListener("click", (e) => {
  document.querySelectorAll(".cart-item").forEach((item) => {
    searchio.track("purchase", item.id);
  });
});

Documentation

For full documentation, see https://sajari-sdk-js.netlify.com/.

License

We use the MIT license