Socket
Socket
Sign inDemoInstall

@sajari/sdk-js

Package Overview
Dependencies
0
Maintainers
7
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @sajari/sdk-js

Search.io JavaScript SDK


Version published
Weekly downloads
5.3K
decreased by-19.13%
Maintainers
7
Install size
807 kB
Created
Weekly downloads
 

Changelog

Source

2.10.8

Patch Changes

  • 61804ba: Fix for when promotions use keys that aren't selected in fields

Readme

Source

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

Keywords

FAQs

Last updated on 13 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc