Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/confirmation.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/funnel.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hellotext/vtex-checkout",
"version": "1.0.3",
"version": "1.0.4",
"description": "Hellotext SDK for VTEX Checkout",
"unpkg": "dist/funnel.js",
"author": "Hellotext",
Expand Down
41 changes: 39 additions & 2 deletions src/__tests__/confirmation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ describe("confirmation.initialize", () => {
},
object_parameters: {
reference: "order-group-123",
amount: 109995000,
currency: "COP",
source: "vtex",
delivery: "deliver",
items: [
expect.objectContaining({
Expand All @@ -122,6 +121,44 @@ describe("confirmation.initialize", () => {
});
});

it("identifies newsletter opt-in from the current order form", () => {
const orderForm = createOrderForm();

orderForm.clientPreferencesData = {
optinNewsLetter: true,
};

getOrderFormMock = jest.fn(() => ({
done: (callback) => callback(orderForm),
}));
global.window.vtexjs.checkout.getOrderForm = getOrderFormMock;

confirmation.initialize("business-123");

const expectedUser = {
id: "test.user@example.com",
email: "test.user@example.com",
first_name: "Test",
last_name: "User",
phone: "+15555550123",
document: "TEST-DOC-12345",
source: "vtex",
subscription_state: true,
};

expect(Hellotext.identify).toHaveBeenCalledWith(
"test.user@example.com",
expectedUser,
);
expect(Hellotext.track).toHaveBeenCalledWith("order.placed", {
user_parameters: expectedUser,
object_parameters: expect.objectContaining({
reference: "order-group-123",
source: "vtex",
}),
});
});

it("only initializes Hellotext when VTEX checkout is unavailable", () => {
delete global.window.vtexjs;
delete global.vtexjs;
Expand Down
36 changes: 30 additions & 6 deletions src/__tests__/funnel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ describe("funnel.initialize", () => {
},
object_parameters: {
reference: "order-group-123",
amount: 109995000,
currency: "COP",
source: "vtex",
delivery: "deliver",
items: [
expect.objectContaining({
Expand Down Expand Up @@ -174,8 +173,7 @@ describe("funnel.initialize", () => {
},
object_parameters: {
reference: "order-group-123",
amount: 109995000,
currency: "COP",
source: "vtex",
delivery: "deliver",
items: [
expect.objectContaining({
Expand Down Expand Up @@ -218,6 +216,33 @@ describe("funnel.initialize", () => {
expect(Hellotext.track).not.toHaveBeenCalled();
});

it("identifies newsletter opt-in changes when VTEX emits an order update event", async () => {
const updatedOrderForm = createOrderForm();

updatedOrderForm.clientPreferencesData = {
optinNewsLetter: true,
};

funnel.initialize("business-123");
await flushAsyncWork();

Hellotext.identify.mockClear();
Hellotext.track.mockClear();
orderFormUpdatedHandler({}, updatedOrderForm);

expect(Hellotext.identify).toHaveBeenCalledWith("test.user@example.com", {
id: "test.user@example.com",
email: "test.user@example.com",
first_name: "Test",
last_name: "User",
phone: "+15555550123",
document: "TEST-DOC-12345",
source: "vtex",
subscription_state: true,
});
expect(Hellotext.track).not.toHaveBeenCalled();
});

it("tracks checkout started once per order form id", async () => {
const updatedOrderForm = createOrderForm();

Expand Down Expand Up @@ -255,8 +280,7 @@ describe("funnel.initialize", () => {
},
object_parameters: {
reference: "order-group-456",
amount: 109995000,
currency: "COP",
source: "vtex",
delivery: "deliver",
items: [
expect.objectContaining({
Expand Down
29 changes: 27 additions & 2 deletions src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ describe("extractUserData", () => {
source: "vtex",
});
});

it("maps VTEX newsletter opt-in into a Hellotext subscribe signal", () => {
const orderForm = createOrderForm();

orderForm.clientPreferencesData = {
optinNewsLetter: true,
};

expect(extractUserData(orderForm)).toEqual(
expect.objectContaining({
subscription_state: true,
}),
);
});

it("does not send a subscription state when VTEX newsletter opt-in is false", () => {
const orderForm = createOrderForm();

orderForm.clientPreferencesData = {
optinNewsLetter: false,
};

expect(extractUserData(orderForm)).not.toHaveProperty(
"subscription_state",
);
});
});

describe("extractOrderData", () => {
Expand All @@ -69,8 +95,7 @@ describe("extractOrderData", () => {

expect(extractOrderData(orderForm)).toEqual({
reference: "order-group-123",
amount: 109995000,
currency: "COP",
source: "vtex",
delivery: "deliver",
items: [
{
Expand Down
12 changes: 9 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const extractUserData = (orderForm) => {
const profile = orderForm?.clientProfileData || {};
const preferences = orderForm?.clientPreferencesData || {};

return {
const user = {
id: profile.email,
email: profile.email,
first_name: profile.firstName,
Expand All @@ -10,6 +11,12 @@ const extractUserData = (orderForm) => {
document: profile.document,
source: "vtex",
};

if (preferences.optinNewsLetter === true) {
user.subscription_state = true;
Comment on lines +15 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the opt-in update after initial identify

When the shopper enters an email/phone before checking the newsletter box, the first funnel update identifies that same user without subscription_state; the later update reaches this new branch, but the bundled Hellotext SDK in dist/funnel.js returns early from identify when the remembered hello_user_id already matches the id, so this added field is never posted. This makes the common “enter identity, then opt in” flow fail to capture the opt-in unless the code forces a non-deduped update path.

Useful? React with 👍 / 👎.

}

return user;
};

const extractOrderData = (orderForm) => {
Expand Down Expand Up @@ -75,8 +82,7 @@ const extractOrderData = (orderForm) => {

return {
reference: orderForm.orderGroup || orderForm.orderFormId,
amount: orderForm.value || 0,
currency: storeData.currencyCode,
source: "vtex",
delivery,
items,
Comment on lines 83 to 87
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep order totals in the tracked order payload

Both checkout.started and order.placed pass this object as object_parameters, but this return value now drops the top-level amount and currency that previously came from orderForm.value and storePreferencesData.currencyCode. As a result Hellotext receives item prices but no order total/currency, which breaks value attribution especially when the cart total differs from item sums due to discounts, shipping, or taxes; add source without removing the existing total fields.

Useful? React with 👍 / 👎.

};
Expand Down