sajari

Official repository of the Sajari SDK for JavaScript integration into web applications

Showing:

Popularity

Downloads/wk

70

GitHub Stars

9

Maintenance

Last Commit

15d ago

Contributors

14

Package

Dependencies

3

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Sajari Javascript SDK

npm (scoped) Netlify Status build size license

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

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

You can also quickly generate search interfaces from the Sajari 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, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";

// 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>
  // 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 Sajari account you can sign up here.

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

Create a session to track the queries being performed via click tracking. In this case we're using q to store the query on the InteractiveSession.

const clickTrackedSession = new InteractiveSession(
  "q",
  new DefaultSession(TrackingType.Click, "url")
);

Perform a search on the specified pipeline and handle the results. Here we're searching our collection using the website pipeline with our tracked session.

const values = { q: "FAQ" };
websitePipeline
  .search(values, clickTrackedSession.next(values))
  .then(([response, values]) => {
    // 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 click tracking. This will redirect the user through the Sajari token endpoint to the real page identified by the result, registering their "click" on the way through.

const values = { q: "FAQ" };
websitePipeline
  .search(values, clickTrackedSession.next(values))
  .then(([response, values]) => {
    response.results.forEach((r) => {
      const title = document.createElement("a");
      title.textContent = r.values.title;
      title.href = r.values.url;
      title.onmousedown = () => {
        title.href = r.token.click;
      };

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

Consuming an interaction token

On each result when using TrackingType.Click or TrackingType.PosNeg, there is a set of tokens. These tokens allow you to provide feedback to the ranking system. When a user interacts with a result, you can send back the token with some extra information.

const { Client } = require("@sajari/sdk-js");

const client = new Client("<account_id>", "<collection_id>", {
  key: "<key_id>",
  secret: "<key_secret>",
});

/*
The following invocation of the consume function, is noting that this particular
interaction was a "purchase" and the user purchasing the item was 20 years old
(this information coming from some system that you operate.)
*/
client.interactionConsume(token, "purchase", 1.0, {
  age: "20",
});

Documentation

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

License

We use the MIT license

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial