
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@sajari/sdk-js
Advanced tools
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.
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>");
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>
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...
});
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...
});
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);
});
});
For full documentation, see https://sajari-sdk-js.netlify.com/.
We use the MIT license
2.10.8
FAQs
Search.io JavaScript SDK
The npm package @sajari/sdk-js receives a total of 3,954 weekly downloads. As such, @sajari/sdk-js popularity was classified as popular.
We found that @sajari/sdk-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.