Skip to content

Commit 38b1bd4

Browse files
authored
[NCPS Hosted buttons] Update FPTI tracking params (#2557)
* fpti add params * Fix issue
1 parent 564931c commit 38b1bd4

File tree

4 files changed

+20
-59
lines changed

4 files changed

+20
-59
lines changed

src/hosted-buttons/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
renderDefaultButton,
1515
renderForm,
1616
renderStandaloneButton,
17-
getTrackingId,
1817
} from "./utils";
1918
import type {
2019
HostedButtonsComponent,
@@ -50,34 +49,37 @@ export const getHostedButtonsComponent = (): HostedButtonsComponent => {
5049
selector,
5150
});
5251

53-
const trackingId = getTrackingId(selector);
52+
const fptiTrackingParams =
53+
window[
54+
`__pp_form_fields_${hostedButtonId}`
55+
]?.getFptiTrackingParams?.() || {};
5456

5557
const createOrder = buildHostedButtonCreateOrder({
5658
enableDPoP,
5759
hostedButtonId,
5860
merchantId,
59-
trackingId,
61+
fptiTrackingParams,
6062
});
6163

6264
const onApprove = buildHostedButtonOnApprove({
6365
enableDPoP,
6466
hostedButtonId,
6567
merchantId,
66-
trackingId,
68+
fptiTrackingParams,
6769
});
6870

6971
const onShippingAddressChange = buildHostedButtonOnShippingAddressChange({
7072
enableDPoP,
7173
hostedButtonId,
7274
shouldIncludeShippingCallbacks,
73-
trackingId,
75+
fptiTrackingParams,
7476
});
7577

7678
const onShippingOptionsChange = buildHostedButtonOnShippingOptionsChange({
7779
enableDPoP,
7880
hostedButtonId,
7981
shouldIncludeShippingCallbacks,
80-
trackingId,
82+
fptiTrackingParams,
8183
});
8284

8385
const buttonOptions: HostedButtonOptions = {

src/hosted-buttons/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export type GetCallbackProps = {|
133133
hostedButtonId: string,
134134
merchantId?: string,
135135
shouldIncludeShippingCallbacks?: boolean,
136-
trackingId?: string,
136+
fptiTrackingParams?: { [key: string]: mixed },
137137
|};
138138

139139
export type HostedButtonsInstance = {|

src/hosted-buttons/utils.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,6 @@ export function getElementFromSelector(
225225
: selector;
226226
}
227227

228-
export function getTrackingId(
229-
HostedButtonSelector: string | HTMLElement
230-
): string {
231-
if (typeof HostedButtonSelector !== "string") {
232-
return "";
233-
}
234-
const ele = document.querySelector(
235-
`${HostedButtonSelector} input[name="uuid"]`
236-
);
237-
return ele ? ele.getAttribute("value") || "" : "";
238-
}
239-
240228
/**
241229
* Attaches form fields (html) to the given selector, and
242230
* initializes window.__pp_form_fields (htmlScript).
@@ -269,7 +257,7 @@ export const buildHostedButtonCreateOrder = ({
269257
enableDPoP,
270258
hostedButtonId,
271259
merchantId,
272-
trackingId,
260+
fptiTrackingParams,
273261
}: GetCallbackProps): CreateOrder => {
274262
return async (data) => {
275263
const userInputs =
@@ -280,15 +268,15 @@ export const buildHostedButtonCreateOrder = ({
280268
const url = `${apiUrl}/v1/checkout/links/${hostedButtonId}/create-context`;
281269
const method = "POST";
282270
const headers = await buildRequestHeaders({ url, method, enableDPoP });
283-
271+
const funding_source = data.paymentSource.toUpperCase();
284272
const response = await request({
285273
url,
286274
// $FlowIssue optional properties are not compatible with [key: string]: string
287275
headers,
288276
method,
289277
body: JSON.stringify({
290278
entry_point: entryPoint,
291-
funding_source: data.paymentSource.toUpperCase(),
279+
funding_source,
292280
merchant_id: merchantId,
293281
...userInputs,
294282
}),
@@ -297,9 +285,10 @@ export const buildHostedButtonCreateOrder = ({
297285
const { body } = response;
298286
getLogger()
299287
.track({
288+
...fptiTrackingParams,
300289
[FPTI_KEY.CONTEXT_ID]: body.context_id,
301290
[FPTI_KEY.EVENT_NAME]: "ncps_create_order",
302-
tracking_id: trackingId,
291+
funding_type: funding_source,
303292
})
304293
.flush();
305294
return body.context_id || onError(body.details?.[0]?.issue || body.name);
@@ -313,7 +302,7 @@ export const buildHostedButtonOnApprove = ({
313302
enableDPoP,
314303
hostedButtonId,
315304
merchantId,
316-
trackingId,
305+
fptiTrackingParams,
317306
}: GetCallbackProps): OnApprove => {
318307
return async (data) => {
319308
const url = `${apiUrl}/v1/checkout/links/${hostedButtonId}/pay`;
@@ -333,9 +322,9 @@ export const buildHostedButtonOnApprove = ({
333322
}).then((response) => {
334323
getLogger()
335324
.track({
325+
...fptiTrackingParams,
336326
[FPTI_KEY.CONTEXT_ID]: data.orderID,
337327
[FPTI_KEY.EVENT_NAME]: "ncps_onapprove_order",
338-
tracking_id: trackingId,
339328
})
340329
.flush();
341330

@@ -359,7 +348,7 @@ export const buildHostedButtonOnShippingAddressChange = ({
359348
enableDPoP,
360349
hostedButtonId,
361350
shouldIncludeShippingCallbacks,
362-
trackingId,
351+
fptiTrackingParams,
363352
}: GetCallbackProps): OnShippingAddressChange | typeof undefined => {
364353
if (shouldIncludeShippingCallbacks) {
365354
return async (data, actions) => {
@@ -398,9 +387,9 @@ export const buildHostedButtonOnShippingAddressChange = ({
398387

399388
getLogger()
400389
.track({
390+
...fptiTrackingParams,
401391
[FPTI_KEY.CONTEXT_ID]: orderID,
402392
[FPTI_KEY.EVENT_NAME]: "ncps_shipping_address_change",
403-
tracking_id: trackingId,
404393
})
405394
.flush();
406395
};
@@ -411,7 +400,7 @@ export const buildHostedButtonOnShippingOptionsChange = ({
411400
enableDPoP,
412401
hostedButtonId,
413402
shouldIncludeShippingCallbacks,
414-
trackingId,
403+
fptiTrackingParams,
415404
}: GetCallbackProps): OnShippingOptionsChange | typeof undefined => {
416405
if (shouldIncludeShippingCallbacks) {
417406
return async (data, actions) => {
@@ -439,9 +428,9 @@ export const buildHostedButtonOnShippingOptionsChange = ({
439428

440429
getLogger()
441430
.track({
431+
...fptiTrackingParams,
442432
[FPTI_KEY.CONTEXT_ID]: orderID,
443433
[FPTI_KEY.EVENT_NAME]: "ncps_shipping_options_change",
444-
tracking_id: trackingId,
445434
})
446435
.flush();
447436
};

src/hosted-buttons/utils.test.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
renderStandaloneButton,
2222
applyContainerStyles,
2323
renderDefaultButton,
24-
getTrackingId,
2524
} from "./utils";
2625

2726
vi.mock("@krakenjs/belter/src", async () => {
@@ -914,35 +913,6 @@ test("getElementFromSelector", () => {
914913
expect(mockQuerySelector).toHaveBeenCalledWith(containerId);
915914
});
916915

917-
describe("getTrackingId", () => {
918-
const containerId = "#container-id";
919-
920-
test("returns uuid value when input element exists and has a value", () => {
921-
const inputElement = document.createElement("input");
922-
inputElement.setAttribute("name", "uuid");
923-
inputElement.setAttribute("value", "test-uuid-123");
924-
925-
const containerElement = document.createElement("div");
926-
containerElement.appendChild(inputElement);
927-
928-
vi.spyOn(document, "querySelector").mockImplementationOnce(
929-
() => inputElement
930-
);
931-
932-
const result = getTrackingId(containerId);
933-
934-
expect(result).toBe("test-uuid-123");
935-
});
936-
937-
test("returns empty string when input element doesn't exist", () => {
938-
vi.spyOn(document, "querySelector").mockImplementationOnce(() => null);
939-
940-
const result = getTrackingId(containerId);
941-
942-
expect(result).toBe("");
943-
});
944-
});
945-
946916
describe("getButtonPreferences", () => {
947917
test("returns all button preferences if all are eligible", () => {
948918
const params = {

0 commit comments

Comments
 (0)