Skip to content

Commit 895924e

Browse files
authored
Merge pull request #347 from 4site-interactive-studios/sticky-nsg
New Components: StickyNSG & StickyPrepopulation
2 parents 591c9c9 + f2501c6 commit 895924e

16 files changed

Lines changed: 791 additions & 3 deletions

packages/scripts/dist/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DonationAmount, DonationFrequency, EnForm, ProcessingFees, Country, } from "./events";
2-
import { AmountLabel, Loader, ProgressBar, UpsellLightbox, ENGrid, OptionsDefaults, setRecurrFreq, PageBackground, MediaAttribution, ApplePay, A11y, CapitalizeFields, Ecard, ClickToExpand, Advocacy, DataAttributes, LiveVariables, iFrame, InputPlaceholders, InputHasValueAndFocus, ShowHideRadioCheckboxes, AutoCountrySelect, SkipToMainContentLink, SrcDefer, NeverBounce, AutoYear, Autocomplete, RememberMe, TranslateFields, ShowIfAmount, EngridLogger, OtherAmount, MinMaxAmount, Ticker, DataReplace, DataHide, AddNameToMessage, ExpandRegionName, AppVersion, UrlToForm, RequiredIfVisible, TidyContact, DataLayer, LiveCurrency, Autosubmit, EventTickets, SwapAmounts, DebugPanel, DebugHiddenFields, FreshAddress, BrandingHtml, CountryDisable, PremiumGift, DigitalWallets, MobileCTA, LiveFrequency, UniversalOptIn, StripeFinancialConnections, GiveBySelect, UrlParamsToBodyAttrs, ExitIntentLightbox, SupporterHub, FastFormFill, SetAttr, ShowIfPresent, ENValidators, CustomCurrency, VGS, PostalCodeValidator, CountryRedirect, WelcomeBack, EcardToTarget, UsOnlyForm, ThankYouPageConditionalContent, EmbeddedEcard, CheckboxLabel, UpsellCheckbox, PostDonationEmbed, FrequencyUpsell, CustomPremium, PreferredPaymentMethod, } from ".";
2+
import { AmountLabel, Loader, ProgressBar, UpsellLightbox, ENGrid, OptionsDefaults, setRecurrFreq, PageBackground, MediaAttribution, ApplePay, A11y, CapitalizeFields, Ecard, ClickToExpand, Advocacy, DataAttributes, LiveVariables, iFrame, InputPlaceholders, InputHasValueAndFocus, ShowHideRadioCheckboxes, AutoCountrySelect, SkipToMainContentLink, SrcDefer, NeverBounce, AutoYear, Autocomplete, RememberMe, TranslateFields, ShowIfAmount, EngridLogger, OtherAmount, MinMaxAmount, Ticker, DataReplace, DataHide, AddNameToMessage, ExpandRegionName, AppVersion, UrlToForm, RequiredIfVisible, TidyContact, DataLayer, LiveCurrency, Autosubmit, EventTickets, SwapAmounts, DebugPanel, DebugHiddenFields, FreshAddress, BrandingHtml, CountryDisable, PremiumGift, DigitalWallets, MobileCTA, LiveFrequency, UniversalOptIn, StripeFinancialConnections, GiveBySelect, UrlParamsToBodyAttrs, ExitIntentLightbox, SupporterHub, FastFormFill, SetAttr, ShowIfPresent, ENValidators, CustomCurrency, VGS, PostalCodeValidator, CountryRedirect, WelcomeBack, EcardToTarget, UsOnlyForm, ThankYouPageConditionalContent, EmbeddedEcard, CheckboxLabel, UpsellCheckbox, PostDonationEmbed, FrequencyUpsell, CustomPremium, StickyNSG, StickyPrepopulation, PreferredPaymentMethod, } from ".";
33
export class App extends ENGrid {
44
constructor(options) {
55
super();
@@ -160,6 +160,8 @@ export class App extends ENGrid {
160160
new Autosubmit();
161161
// Adjust display of event tickets.
162162
new EventTickets();
163+
// StickyNSG - Must load before SwapAmounts
164+
new StickyNSG();
163165
// Swap Amounts
164166
new SwapAmounts();
165167
// On the end of the script, after all subscribers defined, let's load the current frequency
@@ -279,6 +281,7 @@ export class App extends ENGrid {
279281
new CheckboxLabel();
280282
new PostDonationEmbed();
281283
new FrequencyUpsell();
284+
new StickyPrepopulation();
282285
//Debug panel
283286
let showDebugPanel = this.options.Debug;
284287
try {

packages/scripts/dist/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export * from "./optin-ladder";
8686
export * from "./post-donation-embed";
8787
export * from "./frequency-upsell-modal";
8888
export * from "./frequency-upsell";
89+
export * from "./sticky-nsg";
90+
export * from "./sticky-prepopulation";
8991
export * from "./preferred-payment-method";
9092
export * from "./events";
9193
export * from "./version";

packages/scripts/dist/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export * from "./optin-ladder";
8686
export * from "./post-donation-embed";
8787
export * from "./frequency-upsell-modal";
8888
export * from "./frequency-upsell";
89+
export * from "./sticky-nsg";
90+
export * from "./sticky-prepopulation";
8991
export * from "./preferred-payment-method";
9092
// Events
9193
export * from "./events";

packages/scripts/dist/interfaces/options.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface Options {
5050
fieldClearSelectorTargetLocation?: string;
5151
checked?: boolean;
5252
};
53+
StickyNSG?: boolean;
54+
StickyPrepopulation?: false | {
55+
fields: string[];
56+
};
5357
TidyContact?: false | {
5458
cid?: string;
5559
page_types?: ("DONATION" | "ECARD" | "SURVEY" | "EMAILTOTARGET" | "ADVOCACY" | "SUBSCRIBEFORM" | "EVENT" | "SUPPORTERHUB" | "UNSUBSCRIBE" | "TWEETPAGE" | "UNKNOWN")[];

packages/scripts/dist/interfaces/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const OptionsDefaults = {
4141
CountryRedirect: false,
4242
WelcomeBack: false,
4343
OptInLadder: false,
44+
StickyNSG: false,
45+
StickyPrepopulation: false,
4446
PreferredPaymentMethod: false,
4547
PageLayouts: [
4648
"leftleft1col",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare class StickyNSG {
2+
private logger;
3+
private cookieName;
4+
constructor();
5+
private shouldRun;
6+
private nsgActiveOnPage;
7+
private deleteCookieIfGiftProcessComplete;
8+
private createStickyNSGCookie;
9+
private applyStickyNSGCookie;
10+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { EngridLogger } from "./logger";
2+
import { ENGrid } from "./engrid";
3+
import * as cookie from "./cookie";
4+
export class StickyNSG {
5+
constructor() {
6+
this.logger = new EngridLogger("StickyNSG", "teal", "white", "📌");
7+
this.cookieName = "engrid-sticky-nsg";
8+
if (!this.shouldRun())
9+
return;
10+
this.logger.log("Sticky NSG is enabled");
11+
this.deleteCookieIfGiftProcessComplete();
12+
this.createStickyNSGCookie();
13+
this.applyStickyNSGCookie();
14+
}
15+
shouldRun() {
16+
return ENGrid.getOption("StickyNSG") === true;
17+
}
18+
/*
19+
* Determine if NSG provided by EN is active on the page
20+
*/
21+
nsgActiveOnPage() {
22+
return (window.EngagingNetworks &&
23+
window.EngagingNetworks.suggestedGift &&
24+
typeof window.EngagingNetworks.suggestedGift === "object" &&
25+
Object.keys(window.EngagingNetworks.suggestedGift).length > 0);
26+
}
27+
/*
28+
* Delete the cookie if the gift process is complete
29+
*/
30+
deleteCookieIfGiftProcessComplete() {
31+
if (ENGrid.getGiftProcess()) {
32+
this.logger.log("Gift process complete, removing sticky NSG cookie if it exists");
33+
cookie.remove(this.cookieName);
34+
}
35+
}
36+
/*
37+
* Create the sticky NSG cookie if NSG is active on the page
38+
*/
39+
createStickyNSGCookie() {
40+
var _a, _b, _c, _d, _e, _f;
41+
if (!this.nsgActiveOnPage()) {
42+
this.logger.log("No NSG active on page, not creating sticky NSG cookie");
43+
return;
44+
}
45+
const url = new URL(window.location.href);
46+
if (url.searchParams.get("skipstickynsg") === "true") {
47+
this.logger.log("'skipstickynsg' param present, not creating sticky NSG cookie");
48+
return;
49+
}
50+
// We do some reformating to match the EngridAmounts format
51+
// We also add "Other" to the amounts list
52+
const nsg = window.EngagingNetworks.suggestedGift;
53+
this.logger.log("Creating sticky NSG cookie", nsg);
54+
const oneTimeNsg = (_a = nsg.single) === null || _a === void 0 ? void 0 : _a.reduce((acc, curr) => {
55+
acc[curr.value] = curr.value;
56+
return acc;
57+
}, {});
58+
const oneTimeDefault = (_c = (_b = nsg.single) === null || _b === void 0 ? void 0 : _b.find((gift) => gift.nextSuggestedGift)) === null || _c === void 0 ? void 0 : _c.value;
59+
const recurringNsg = (_d = nsg.recurring) === null || _d === void 0 ? void 0 : _d.reduce((acc, curr) => {
60+
acc[curr.value] = curr.value;
61+
return acc;
62+
}, {});
63+
const recurringDefault = (_f = (_e = nsg.recurring) === null || _e === void 0 ? void 0 : _e.find((gift) => gift.nextSuggestedGift)) === null || _f === void 0 ? void 0 : _f.value;
64+
const nsgCookieData = {};
65+
if (oneTimeNsg && oneTimeDefault) {
66+
nsgCookieData.onetime = {
67+
amounts: oneTimeNsg,
68+
default: oneTimeDefault,
69+
stickyDefault: false,
70+
};
71+
}
72+
if (recurringNsg && recurringDefault) {
73+
nsgCookieData.monthly = {
74+
amounts: recurringNsg,
75+
default: recurringDefault,
76+
stickyDefault: false,
77+
};
78+
}
79+
if (Object.keys(nsgCookieData).length === 0) {
80+
this.logger.log("No valid NSG data found to create sticky NSG cookie");
81+
return;
82+
}
83+
const cookieValue = JSON.stringify(nsgCookieData);
84+
cookie.set(this.cookieName, cookieValue, { path: "/", expires: 30 });
85+
this.logger.log("Sticky NSG cookie created", cookieValue);
86+
}
87+
/*
88+
* Apply the sticky NSG cookie values to window.EngridAmounts if NSG is not active on the page
89+
*/
90+
applyStickyNSGCookie() {
91+
if (this.nsgActiveOnPage()) {
92+
this.logger.log("NSG active on page, not applying sticky NSG cookie, leaving the EN NSG values.");
93+
return;
94+
}
95+
const cookieValue = cookie.get(this.cookieName);
96+
if (!cookieValue) {
97+
this.logger.log("No sticky NSG cookie found, nothing to apply");
98+
return;
99+
}
100+
try {
101+
const nsg = JSON.parse(cookieValue);
102+
this.logger.log("Applying sticky NSG cookie values", nsg);
103+
window.EngridAmounts = nsg;
104+
}
105+
catch (e) {
106+
this.logger.error("Error parsing sticky NSG cookie, not applying", e);
107+
}
108+
}
109+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export declare class StickyPrepopulation {
2+
private logger;
3+
private options;
4+
private cookieName;
5+
constructor();
6+
private shouldRun;
7+
private deleteCookie;
8+
private createCookie;
9+
private applyPrepopulation;
10+
private getSupporterDetailsFromFields;
11+
private encryptSupporterDetails;
12+
private decryptSupporterDetails;
13+
private createEncryptionKey;
14+
private arrayBufferToBase64;
15+
private base64ToArrayBuffer;
16+
private getSeed;
17+
}

0 commit comments

Comments
 (0)