Update by npm
First of all, we need to update to newest version by running
npm install --save @sajari/[email protected]
Update search function
The search function is the most important function of sajari, it has been changed the way to use from version 1.x.x
to version 2.x.x
This is an example how it looks with version 1.x.x
import { Client, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";
const websitePipeline = new Client("<project>", "<collection>").pipeline(
"website"
);
websitePipeline.search(
{ q: "FAQ" },
(error, response, values) => {
// Check for error
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);
});
}
);
As you can see that, the search
function includes response handling right inside it.
But this is how it looks on version 2.x.x
import { Client } from "@sajari/sdk-js";
const websitePipeline = new Client("<project>", "<collection>").pipeline("website");
websitePipeline.search(
{ q: "FAQ" },
).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...
});
On version 2.x.x
, handling response has been moved out of search
function.
Tracking event
Version 1.x.x
import { Client, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";
const websitePipeline = new Client("<project>", "<collection>").pipeline(
"website"
);
const clickTrackedSession = new InteractiveSession(
"q",
new DefaultSession(TrackingType.Click, "url", {})
);
websitePipeline.search(
{ q: "FAQ" },
clickTrackedSession,
(error, response, values) => {
// Check for error
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);
});
}
);
Version 2.x.x
import { Client, SearchIOAnalytics } from "@sajari/sdk-js";
const pipeline = new Client("<account_id>", "<collection_id>").pipeline("app");
const searchio = new SearchIOAnalytics("<account_id>", "<collection_id>");
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...
});
Documentation
Version 1: https://github.com/sajari/sdk-js/tree/v1
Version 2: https://github.com/sajari/sdk-js
Add new comment