@rebilly/instruments 3.39.4 → 3.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebilly/instruments",
3
- "version": "3.39.4",
3
+ "version": "3.40.0",
4
4
  "author": "Rebilly",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -10,6 +10,7 @@ import { fetchAccountAndWebsite as FetchAccountAndWebsite } from '../../storefro
10
10
  import { fetchPaymentInstrument as FetchInstruments } from '../../storefront/payment-instruments';
11
11
  import { fetchDepositRequest as FetchDepositRequest } from '../../storefront/deposit-requests';
12
12
  import DepositRequestModel from '../../storefront/models/deposit-request-model';
13
+ import { getLeadSourceData } from './get-lead-source-data';
13
14
 
14
15
  export class DataInstance {
15
16
  constructor({
@@ -116,7 +117,8 @@ export class DataInstance {
116
117
  hasAmountDue: this.hasAmountDue,
117
118
  summaryItems: this.summaryItems,
118
119
  summaryLineItems: this.summaryLineItems,
119
- isShippingRequired: this.isShippingRequired
120
+ isShippingRequired: this.isShippingRequired,
121
+ leadSource: getLeadSourceData(),
120
122
  }))
121
123
  }
122
124
  }
@@ -0,0 +1,46 @@
1
+ const collectedProperties = [
2
+ // Google UTM fields
3
+ 'utm_source',
4
+ 'utm_medium',
5
+ 'utm_campaign',
6
+ 'utm_term',
7
+ 'utm_content',
8
+ // FramePay specific fields
9
+ 'affiliate',
10
+ 'subAffiliate',
11
+ 'clickId',
12
+ 'salesAgent'
13
+ ];
14
+ const prefix = /utm_/g;
15
+
16
+ /**
17
+ * Generates the lead source data object that framepay expects as part of
18
+ * the extraData property. We generate it in the top level of instruments
19
+ * so that it has the correct path and query parameters.
20
+ * @returns {Object} The lead source data object
21
+ */
22
+ export const getLeadSourceData = () => {
23
+ const { href, origin, pathname, search } = window.location;
24
+
25
+ const leadSource = {
26
+ // Large query parameters including JWTs will cause issues, so make sure we dont
27
+ // use anything longer than 255 characters.
28
+ path: href.length <= 255 ? href : `${origin}${pathname}`
29
+ };
30
+
31
+ const query = search.replace('?', '');
32
+
33
+ // Process query parameters and add them to the leadSource object
34
+ return query
35
+ .split('&')
36
+ .map((field) => field.split('='))
37
+ .filter((field) => {
38
+ const [name] = field;
39
+ return collectedProperties.indexOf(name) > -1;
40
+ })
41
+ .reduce((data, field) => {
42
+ const [name, value = null] = field;
43
+ data[name.replace(prefix, '')] = value;
44
+ return data;
45
+ }, leadSource);
46
+ };
@@ -0,0 +1,38 @@
1
+ import { getLeadSourceData } from './get-lead-source-data';
2
+
3
+ export const mockLocation = (url) => {
4
+ const location = new URL(url);
5
+ delete window.location;
6
+ window.location = location;
7
+ };
8
+
9
+ describe('getLeadSourceData', () => {
10
+ it('should return an object with a path property that matches the current URL', () => {
11
+ const result = getLeadSourceData();
12
+ expect(result.path).toBe(window.location.href);
13
+ });
14
+
15
+ it('should return the path without the query if the query is longer than 255 characters', () => {
16
+ const query = Array(300).fill('a').join('');
17
+
18
+ const pathWithoutQuery = 'http://www.example.com/path';
19
+ mockLocation(`${pathWithoutQuery}?${query}`);
20
+
21
+ const result = getLeadSourceData();
22
+ expect(result.path).toBe(pathWithoutQuery);
23
+ });
24
+
25
+ it('should add the query parameters to the returned object', () => {
26
+ const query = 'utm_source=google&utm_medium=cpc&utm_campaign=example';
27
+ mockLocation(`http://www.example.com/path?${query}`);
28
+
29
+ expect(getLeadSourceData()).toMatchInlineSnapshot(`
30
+ Object {
31
+ "campaign": "example",
32
+ "medium": "cpc",
33
+ "path": "http://www.example.com/path?utm_source=google&utm_medium=cpc&utm_campaign=example",
34
+ "source": "google",
35
+ }
36
+ `);
37
+ });
38
+ });