@4site/engrid-scripts 0.22.14 → 0.22.16

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.
@@ -8,6 +8,13 @@ export declare class FrequencyUpsell {
8
8
  private _form;
9
9
  private modalSeen;
10
10
  constructor();
11
+ /**
12
+ * Select the proper options (single config or A/B variant) and return a concrete FrequencyUpsellOptions object.
13
+ * If an A/B test config is provided (abTest: true, options: [...]) a random variant is chosen and stored
14
+ * in a 1-day cookie so subsequent visits get the same variant.
15
+ */
16
+ private selectOptions;
17
+ private randomIndex;
11
18
  /**
12
19
  * Check if the FrequencyUpsell should run:
13
20
  * - Check if the FrequencyUpsell is enabled in the window object
@@ -5,6 +5,7 @@
5
5
  * See FrequencyUpsellOptions for more details.
6
6
  */
7
7
  import { DonationAmount, DonationFrequency, EnForm, EngridLogger, FrequencyUpsellModal, FrequencyUpsellOptionsDefaults, ProcessingFees, } from ".";
8
+ import * as cookie from "./cookie";
8
9
  export class FrequencyUpsell {
9
10
  constructor() {
10
11
  this.logger = new EngridLogger("FrequencyUpsell", "lightgray", "darkblue", "🏦");
@@ -19,12 +20,55 @@ export class FrequencyUpsell {
19
20
  this.logger.log("FrequencyUpsell not running");
20
21
  return;
21
22
  }
22
- this.options = Object.assign(Object.assign({}, FrequencyUpsellOptionsDefaults), window.EngridFrequencyUpsell);
23
+ this.options = this.selectOptions(window.EngridFrequencyUpsell);
23
24
  this.logger.log("FrequencyUpsell initialized", this.options);
24
25
  this.upsellModal = new FrequencyUpsellModal(this.options);
25
26
  this.createFrequencyField();
26
27
  this.addEventListeners();
27
28
  }
29
+ /**
30
+ * Select the proper options (single config or A/B variant) and return a concrete FrequencyUpsellOptions object.
31
+ * If an A/B test config is provided (abTest: true, options: [...]) a random variant is chosen and stored
32
+ * in a 1-day cookie so subsequent visits get the same variant.
33
+ */
34
+ selectOptions(config) {
35
+ // Simple (non AB) case
36
+ if (!config.abTest) {
37
+ return Object.assign(Object.assign({}, FrequencyUpsellOptionsDefaults), config);
38
+ }
39
+ const abConfig = config;
40
+ const cookieName = abConfig.cookieName || "engrid_frequency_upsell_variant";
41
+ const existing = cookie.get(cookieName);
42
+ let index;
43
+ if (existing !== undefined) {
44
+ const parsed = parseInt(existing, 10);
45
+ if (!isNaN(parsed) && parsed >= 0 && parsed < abConfig.options.length) {
46
+ index = parsed;
47
+ }
48
+ else {
49
+ index = this.randomIndex(abConfig.options.length);
50
+ }
51
+ }
52
+ else {
53
+ index = this.randomIndex(abConfig.options.length);
54
+ }
55
+ // Persist for configured duration
56
+ const duration = abConfig.cookieDurationDays || 1;
57
+ cookie.set(cookieName, index.toString(), { expires: duration });
58
+ const chosen = abConfig.options[index];
59
+ // Push variant info to dataLayer if available
60
+ if (window.dataLayer) {
61
+ window.dataLayer.push({
62
+ event: "frequency_upsell_ab_variant",
63
+ frequencyUpsellVariantIndex: index,
64
+ frequencyUpsellVariantTitle: chosen.title,
65
+ });
66
+ }
67
+ return Object.assign(Object.assign({}, FrequencyUpsellOptionsDefaults), chosen);
68
+ }
69
+ randomIndex(length) {
70
+ return Math.floor(Math.random() * length);
71
+ }
28
72
  /**
29
73
  * Check if the FrequencyUpsell should run:
30
74
  * - Check if the FrequencyUpsell is enabled in the window object
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { Options, OptionsDefaults } from "./interfaces/options";
3
3
  export { UpsellOptions, UpsellOptionsDefaults, } from "./interfaces/upsell-options";
4
4
  export { TranslateOptions, TranslateOptionsDefaults, } from "./interfaces/translate-options";
5
5
  export { ExitIntentOptions, ExitIntentOptionsDefaults, } from "./interfaces/exit-intent-options";
6
- export { FrequencyUpsellOptions, FrequencyUpsellOptionsDefaults, } from "./interfaces/frequency-upsell-options";
6
+ export { FrequencyUpsellOptions, FrequencyUpsellOptionsDefaults, FrequencyUpsellABTestConfig, FrequencyUpsellConfig, } from "./interfaces/frequency-upsell-options";
7
7
  export * from "./loader";
8
8
  export * from "./app";
9
9
  export * from "./amount-label";
@@ -11,4 +11,11 @@ export interface FrequencyUpsellOptions {
11
11
  onAccept: () => void;
12
12
  onDecline: () => void;
13
13
  }
14
+ export interface FrequencyUpsellABTestConfig {
15
+ abTest: true;
16
+ options: FrequencyUpsellOptions[];
17
+ cookieName?: string;
18
+ cookieDurationDays?: number;
19
+ }
20
+ export type FrequencyUpsellConfig = FrequencyUpsellOptions | FrequencyUpsellABTestConfig;
14
21
  export declare const FrequencyUpsellOptionsDefaults: FrequencyUpsellOptions;
@@ -15,6 +15,7 @@ export interface Options {
15
15
  MaxAmount?: number;
16
16
  MinAmountMessage?: string;
17
17
  MaxAmountMessage?: string;
18
+ UseAmountValidatorFromEN?: boolean;
18
19
  SkipToMainContentLink?: boolean;
19
20
  SrcDefer?: boolean;
20
21
  NeverBounceAPI?: string | null;
@@ -14,6 +14,7 @@ export const OptionsDefaults = {
14
14
  MaxAmount: 100000,
15
15
  MinAmountMessage: "Amount must be at least $1",
16
16
  MaxAmountMessage: "Amount must be less than $100,000",
17
+ UseAmountValidatorFromEN: false,
17
18
  SkipToMainContentLink: true,
18
19
  SrcDefer: true,
19
20
  NeverBounceAPI: null,
@@ -1,13 +1,16 @@
1
1
  export declare class MinMaxAmount {
2
2
  private _form;
3
3
  private _amount;
4
+ private _frequency;
4
5
  private minAmount;
5
6
  private maxAmount;
6
7
  private minAmountMessage;
7
8
  private maxAmountMessage;
9
+ private enAmountValidator;
8
10
  private logger;
9
11
  constructor();
10
12
  shouldRun(): boolean;
11
13
  enOnValidate(): void;
12
14
  liveValidate(): void;
15
+ private setValidationConfigFromEN;
13
16
  }
@@ -1,19 +1,22 @@
1
1
  // This script adds an erros message to the page if the amount is greater than the max amount or less than the min amount.
2
- import { DonationAmount, EnForm, ENGrid, EngridLogger } from ".";
2
+ import { DonationAmount, DonationFrequency, EnForm, ENGrid, EngridLogger, } from ".";
3
3
  export class MinMaxAmount {
4
4
  constructor() {
5
5
  var _a, _b;
6
6
  this._form = EnForm.getInstance();
7
7
  this._amount = DonationAmount.getInstance();
8
+ this._frequency = DonationFrequency.getInstance();
8
9
  this.minAmount = (_a = ENGrid.getOption("MinAmount")) !== null && _a !== void 0 ? _a : 1;
9
10
  this.maxAmount = (_b = ENGrid.getOption("MaxAmount")) !== null && _b !== void 0 ? _b : 100000;
10
11
  this.minAmountMessage = ENGrid.getOption("MinAmountMessage");
11
12
  this.maxAmountMessage = ENGrid.getOption("MaxAmountMessage");
13
+ this.enAmountValidator = null;
12
14
  this.logger = new EngridLogger("MinMaxAmount", "white", "purple", "🔢");
13
15
  if (!this.shouldRun()) {
14
16
  // If we're not on a Donation Page, get out
15
17
  return;
16
18
  }
19
+ this.setValidationConfigFromEN();
17
20
  this._amount.onAmountChange.subscribe((s) => window.setTimeout(this.liveValidate.bind(this), 1000) // Wait 1 second for the amount to be updated
18
21
  );
19
22
  this._form.onValidate.subscribe(this.enOnValidate.bind(this));
@@ -68,4 +71,56 @@ export class MinMaxAmount {
68
71
  ENGrid.removeError(".en__field--withOther");
69
72
  }
70
73
  }
74
+ setValidationConfigFromEN() {
75
+ if (!ENGrid.getOption("UseAmountValidatorFromEN") ||
76
+ !window.EngagingNetworks.validators) {
77
+ this.logger.log("Not setting validation config from EN.");
78
+ return;
79
+ }
80
+ // Find the amount validator for the donation amount field
81
+ // It should be of type "AMNT" or "FAMNT" and have
82
+ // a componentId that matches the donation amount field.
83
+ this.enAmountValidator = window.EngagingNetworks.validators.find((validator) => {
84
+ var _a;
85
+ return ((validator.type === "FAMNT" || validator.type === "AMNT") &&
86
+ ((_a = document
87
+ .querySelector(".en__field--" + validator.componentId)) === null || _a === void 0 ? void 0 : _a.classList.contains("en__field--donationAmt")));
88
+ });
89
+ if (!this.enAmountValidator || !this.enAmountValidator.format) {
90
+ return;
91
+ }
92
+ this.logger.log(`Detected an amount validator for donation amount on the page:`, this.enAmountValidator);
93
+ // Static amount validator
94
+ if (this.enAmountValidator.type === "AMNT") {
95
+ this.minAmount = Number(this.enAmountValidator.format.split("~")[0]);
96
+ this.maxAmount = Number(this.enAmountValidator.format.split("~")[1]);
97
+ this.minAmountMessage = this.enAmountValidator.errorMessage;
98
+ this.maxAmountMessage = this.enAmountValidator.errorMessage;
99
+ this.logger.log(`Setting new values - Min Amount: ${this.minAmount}, Max Amount: ${this.maxAmount}, Error Message: ${this.minAmountMessage}`);
100
+ }
101
+ // Frequency-based amount validator
102
+ if (this.enAmountValidator.type === "FAMNT") {
103
+ this._frequency.onFrequencyChange.subscribe((freq) => {
104
+ if (!this.enAmountValidator || !this.enAmountValidator.format)
105
+ return;
106
+ // In the validator, "onetime" is written as "SINGLE"
107
+ // Validator format for FAMNT is like SINGLE:10~100000|MONTHLY:5~100000|QUARTERLY:25~100000|ANNUAL:25~100000
108
+ const frequency = freq === "onetime" ? "SINGLE" : freq.toUpperCase();
109
+ const validationRange = this.enAmountValidator.format
110
+ .split("|")
111
+ .find((range) => range.startsWith(frequency));
112
+ if (!validationRange) {
113
+ this.logger.log(`No validation range found for frequency: ${frequency}`);
114
+ return;
115
+ }
116
+ const amounts = validationRange.split(":")[1].split("~");
117
+ this.minAmount = Number(amounts[0]);
118
+ this.maxAmount = Number(amounts[1]);
119
+ this.minAmountMessage = this.enAmountValidator.errorMessage;
120
+ this.maxAmountMessage = this.enAmountValidator.errorMessage;
121
+ this.logger.log(`Frequency changed to ${frequency}, updating min and max amounts`, validationRange);
122
+ this.logger.log(`Setting new values - Min Amount: ${this.minAmount}, Max Amount: ${this.maxAmount}, Error Message: ${this.minAmountMessage}`);
123
+ });
124
+ }
125
+ }
71
126
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AppVersion = "0.22.14";
1
+ export declare const AppVersion = "0.22.16";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const AppVersion = "0.22.14";
1
+ export const AppVersion = "0.22.16";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4site/engrid-scripts",
3
- "version": "0.22.14",
3
+ "version": "0.22.16",
4
4
  "description": "Scripts for the ENGRID Project",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "strongly-typed-events": "^2.0.9"
26
26
  },
27
27
  "homepage": "https://github.com/4site-interactive-studios/engrid#readme",
28
- "gitHead": "83a15dbeaa859e1952497ae2e932a630df690eb8",
28
+ "gitHead": "61ace9a9231137c17ba9d180b45bd0634c811871",
29
29
  "devDependencies": {
30
30
  "nodemon": "^2.0.20"
31
31
  }