Skip to content

Commit

Permalink
feat: send event to is (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jul 10, 2023
1 parent e8eb7cc commit 5491afb
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 135 deletions.
1 change: 1 addition & 0 deletions actions/vtex/analytics/sendEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "deco-sites/std/packs/vtex/actions/analytics/sendEvent.ts";
2 changes: 2 additions & 0 deletions live.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import * as $$$$$$$$$$$12 from "./actions/vtex/cart/removeItemAttachment.ts";
import * as $$$$$$$$$$$13 from "./actions/vtex/newsletter/subscribe.ts";
import * as $$$$$$$$$$$14 from "./actions/vtex/wishlist/removeItem.ts";
import * as $$$$$$$$$$$15 from "./actions/vtex/wishlist/addItem.ts";
import * as $$$$$$$$$$$16 from "./actions/vtex/analytics/sendEvent.ts";
import * as $live_workflows from "$live/routes/live/workflows/run.ts";
import * as $live_middleware from "$live/routes/_middleware.ts";
import * as $live_workbench from "$live/routes/live/workbench.ts";
Expand Down Expand Up @@ -240,6 +241,7 @@ const manifest = {
"$live/actions/workflows/cancel.ts": i1$$$$$$$0,
"$live/actions/workflows/signal.ts": i1$$$$$$$1,
"$live/actions/workflows/start.ts": i1$$$$$$$2,
"deco-sites/std/actions/vtex/analytics/sendEvent.ts": $$$$$$$$$$$16,
"deco-sites/std/actions/vtex/cart/addItems.ts": $$$$$$$$$$$6,
"deco-sites/std/actions/vtex/cart/getInstallment.ts": $$$$$$$$$$$8,
"deco-sites/std/actions/vtex/cart/removeItemAttachment.ts": $$$$$$$$$$$12,
Expand Down
5 changes: 3 additions & 2 deletions packs/vtex/accounts/vtex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FnContext } from "$live/types.ts";
import type { Account as AccountBlock } from "$live/blocks/account.ts";
import type { FnContext } from "$live/types.ts";
import type { Manifest } from "deco-sites/std/live.gen.ts";

export interface Account extends AccountBlock {
/**
Expand Down Expand Up @@ -34,7 +35,7 @@ export interface Account extends AccountBlock {

export type Context = FnContext<{
configVTEX?: Account;
}>;
}, Manifest>;

function account(acc: Account) {
return acc;
Expand Down
58 changes: 58 additions & 0 deletions packs/vtex/actions/analytics/sendEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Intelligent Search analytics integration
import type { Context } from "deco-sites/std/packs/vtex/accounts/vtex.ts";
import { fetchSafe } from "deco-sites/std/utils/fetch.ts";
import { getOrSetISCookie } from "../../utils/intelligentSearch.ts";
import { paths } from "../../utils/paths.ts";

export type Props =
| {
type: "session.ping";
}
| {
type: "search.click";
position: number;
text: string;
productId: string;
url: string;
}
| {
type: "search.query";
text: string;
misspelled: boolean;
match: number;
operator: string;
locale: string;
};

/**
* @docs https://developers.vtex.com/docs/api-reference/checkout-api#post-/api/checkout/pub/orderForm/-orderFormId-/items
*/
const action = async (
props: Props,
req: Request,
ctx: Context,
): Promise<null> => {
const { configVTEX: config } = ctx;

const { anonymous, session } = getOrSetISCookie(req, ctx.response.headers);

await fetchSafe(
paths(config!)["event-api"].v1.account.event,
{
method: "POST",
body: JSON.stringify({
...props,
agent: "deco-sites/std",
anonymous,
session,
}),
headers: {
"content-type": "application/json",
},
},
);

return null;
};

export default action;
17 changes: 6 additions & 11 deletions packs/vtex/loaders/intelligentSearch/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,18 @@ const loader = async (
/** Intelligent search API analytics. Fire and forget 🔫 */
const fullTextTerm = params.get("query");
if (fullTextTerm) {
fetchAPI(vtex["event-api"].v1.account.event, {
method: "POST",
body: JSON.stringify({
ctx.invoke("deco-sites/std/actions/vtex/analytics/sendEvent.ts", {
type: "session.ping",
}).then(() =>
ctx.invoke("deco-sites/std/actions/vtex/analytics/sendEvent.ts", {
type: "search.query",
text: fullTextTerm,
misspelled: productsResult.correction?.misspelled ?? false,
match: productsResult.recordsFiltered,
operator: productsResult.operator,
locale: config?.defaultLocale,
agent: "deco-sites/std",
anonymous: crypto.randomUUID(),
session: crypto.randomUUID(),
}),
headers: {
"content-type": "application/json",
},
}).catch(console.error);
})
).catch(console.error);
}

const { products: vtexProducts, pagination, recordsFiltered } =
Expand Down
45 changes: 43 additions & 2 deletions packs/vtex/utils/intelligentSearch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Sort } from "deco-sites/std/packs/vtex/types.ts";
import type { Context } from "deco-sites/std/packs/vtex/accounts/vtex.ts";
import type { SelectedFacet } from "deco-sites/std/packs/vtex/types.ts";
import type { SelectedFacet, Sort } from "deco-sites/std/packs/vtex/types.ts";
import { getCookies, setCookie } from "std/http/mod.ts";

export const SESSION_COOKIE = "vtex_is_session";
export const ANONYMOUS_COOKIE = "vtex_is_anonymous";

const POLICY_KEY = "trade-policy";
const REGION_KEY = "region-id";
Expand Down Expand Up @@ -65,3 +68,41 @@ export const withDefaultParams = ({
false
}`,
});

export const getOrSetISCookie = (req: Request, headers: Headers) => {
const cookies = getCookies(req.headers);

let anonymous = cookies[ANONYMOUS_COOKIE];
let session = cookies[SESSION_COOKIE];

if (!anonymous) {
anonymous = crypto.randomUUID();

setCookie(headers, {
value: anonymous,
name: ANONYMOUS_COOKIE,
path: "/",
secure: true,
httpOnly: true,
maxAge: 365 * 24 * 3600,
});
}

if (!session) {
session = crypto.randomUUID();

setCookie(headers, {
value: session,
name: SESSION_COOKIE,
path: "/",
secure: true,
httpOnly: true,
maxAge: 30 * 60,
});
}

return {
anonymous,
session,
};
};
Loading

0 comments on commit 5491afb

Please sign in to comment.