@saasquatch/squatch-js 2.4.2 → 2.4.3-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.
@@ -22,6 +22,45 @@ function _extends() {
22
22
  return _extends.apply(this, arguments);
23
23
  }
24
24
 
25
+ function _objectWithoutPropertiesLoose(source, excluded) {
26
+ if (source == null) return {};
27
+ var target = {};
28
+ var sourceKeys = Object.keys(source);
29
+ var key, i;
30
+
31
+ for (i = 0; i < sourceKeys.length; i++) {
32
+ key = sourceKeys[i];
33
+ if (excluded.indexOf(key) >= 0) continue;
34
+ target[key] = source[key];
35
+ }
36
+
37
+ return target;
38
+ }
39
+
40
+ function doQuery(url, query, variables, token) {
41
+ const headers = _extends({
42
+ Accept: "application/json"
43
+ }, token ? {
44
+ Authorization: `Bearer ${token}`
45
+ } : {}, {
46
+ "X-SaaSquatch-Referrer": window ? window.location.href : ""
47
+ });
48
+
49
+ const request = superagent.post(url).send({
50
+ query,
51
+ variables
52
+ }).set(headers);
53
+ return thenableSuperagent(request).then(response => response, error => {
54
+ let json;
55
+
56
+ try {
57
+ json = JSON.parse(error.response.text);
58
+ } catch (e) {
59
+ }
60
+
61
+ throw json;
62
+ });
63
+ }
25
64
  function doGet(url, jwt = "") {
26
65
  const headers = {
27
66
  Accept: "application/json",
@@ -164,6 +203,10 @@ function validateWidgetConfig(raw) {
164
203
 
165
204
  return raw;
166
205
  }
206
+ function validatePasswordlessConfig(raw) {
207
+ if (!isObject$1(raw)) throw new Error("Widget properties must be an object");
208
+ return raw;
209
+ }
167
210
 
168
211
  /**
169
212
  *
@@ -286,7 +329,7 @@ class WidgetApi {
286
329
 
287
330
  render(params) {
288
331
  const raw = params;
289
- const clean = validateWidgetConfig(raw);
332
+ const clean = validatePasswordlessConfig(raw);
290
333
  const {
291
334
  widgetType,
292
335
  engagementMedium = "POPUP",
@@ -294,17 +337,42 @@ class WidgetApi {
294
337
  user
295
338
  } = clean;
296
339
  const tenantAlias = encodeURIComponent(this.tenantAlias);
297
- const accountId = encodeURIComponent(user.accountId);
298
- const userId = encodeURIComponent(user.id);
299
-
300
- const optionalParams = _buildParams({
301
- widgetType,
302
- engagementMedium
303
- });
304
-
305
- const path = `/api/v1/${tenantAlias}/widget/account/${accountId}/user/${userId}/render${optionalParams}`;
340
+ const accountId = user ? encodeURIComponent(user.accountId) : null;
341
+ const userId = user ? encodeURIComponent(user.id) : null;
342
+ const query = `
343
+ query renderWidget ($user: UserIdInput, $engagementMedium: UserEngagementMedium, $widgetType: WidgetType) {
344
+ renderWidget(user: $user, engagementMedium: $engagementMedium, widgetType: $widgetType) {
345
+ template
346
+ user {
347
+ id
348
+ accountId
349
+ }
350
+ jsOptions
351
+ widgetConfig {
352
+ values
353
+ }
354
+ }
355
+ }
356
+ `;
357
+ const path = `/api/v1/${tenantAlias}/graphql`;
306
358
  const url = this.domain + path;
307
- return doGet(url, jwt);
359
+ return new Promise(async (resolve, reject) => {
360
+ try {
361
+ var _res$body, _res$body$data;
362
+
363
+ const res = await doQuery(url, query, {
364
+ user: userId && accountId ? {
365
+ id: userId,
366
+ accountId
367
+ } : null,
368
+ engagementMedium,
369
+ widgetType
370
+ }, jwt);
371
+ resolve(res == null ? void 0 : (_res$body = res.body) == null ? void 0 : (_res$body$data = _res$body.data) == null ? void 0 : _res$body$data.renderWidget);
372
+ } catch (e) {
373
+ reject(e);
374
+ }
375
+ });
308
376
  }
309
377
  /**
310
378
  * An API call to send out referral invites to contacts
@@ -338,21 +406,18 @@ class WidgetApi {
338
406
  /**
339
407
  * Looks up the referral code of the current user, if there is any.
340
408
  *
341
- * @return {Promise<ReferralCookie>} code referral code if true.
409
+ * @return {Promise<Object>} code referral code if true.
342
410
  */
343
411
 
344
412
 
345
- async squatchReferralCookie() {
413
+ squatchReferralCookie() {
346
414
  const tenantAlias = encodeURIComponent(this.tenantAlias);
347
415
 
348
- const _saasquatch = Cookies.get("_saasquatch") || "";
416
+ const _saasquatch = Cookies.get("_saasquatch");
349
417
 
350
418
  const cookie = _saasquatch ? `?cookies=${encodeURIComponent(_saasquatch)}` : ``;
351
419
  const url = `${this.domain}/a/${tenantAlias}/widgets/squatchcookiejson${cookie}`;
352
- const response = await doGet(url);
353
- return Promise.resolve(_extends({}, response, {
354
- encodedCookie: _saasquatch
355
- }));
420
+ return doGet(url);
356
421
  }
357
422
 
358
423
  } // builds a param string for widgets
@@ -421,7 +486,7 @@ class AnalyticsApi {
421
486
  // @ts-check
422
487
  /** @hidden */
423
488
 
424
- const _log$6 = debug__default("squatch-js:widget");
489
+ const _log$7 = debug__default("squatch-js:widget");
425
490
  /*
426
491
  * The Widget class is the base class for the different widget types available
427
492
  *
@@ -434,7 +499,7 @@ const _log$6 = debug__default("squatch-js:widget");
434
499
 
435
500
  class Widget {
436
501
  constructor(params) {
437
- _log$6("widget initializing ...");
502
+ _log$7("widget initializing ...");
438
503
 
439
504
  this.content = params.content === "error" ? this._error(params.rsCode) : params.content;
440
505
  this.type = params.type;
@@ -486,9 +551,9 @@ class Widget {
486
551
  }
487
552
 
488
553
  this.analyticsApi.pushAnalyticsLoadEvent(params).then(response => {
489
- _log$6(`${params.engagementMedium} loaded event recorded.`);
554
+ _log$7(`${params.engagementMedium} loaded event recorded.`);
490
555
  }).catch(ex => {
491
- _log$6(new Error(`pushAnalyticsLoadEvent() ${ex}`));
556
+ _log$7(new Error(`pushAnalyticsLoadEvent() ${ex}`));
492
557
  });
493
558
  }
494
559
 
@@ -501,9 +566,9 @@ class Widget {
501
566
  engagementMedium: sqh.mode.widgetMode,
502
567
  shareMedium: medium
503
568
  }).then(response => {
504
- _log$6(`${sqh.mode.widgetMode} share ${medium} event recorded. ${response}`);
569
+ _log$7(`${sqh.mode.widgetMode} share ${medium} event recorded. ${response}`);
505
570
  }).catch(ex => {
506
- _log$6(new Error(`pushAnalyticsLoadEvent() ${ex}`));
571
+ _log$7(new Error(`pushAnalyticsLoadEvent() ${ex}`));
507
572
  });
508
573
  }
509
574
  }
@@ -516,9 +581,9 @@ class Widget {
516
581
  userId: sqh.analytics.attributes.userId,
517
582
  emailList
518
583
  }).then(response => {
519
- _log$6(`Sent email invites to share ${emailList}. ${response}`);
584
+ _log$7(`Sent email invites to share ${emailList}. ${response}`);
520
585
  }).catch(ex => {
521
- _log$6(new Error(`invite() ${ex}`));
586
+ _log$7(new Error(`invite() ${ex}`));
522
587
  });
523
588
  }
524
589
  }
@@ -607,6 +672,7 @@ class Widget {
607
672
  email: email || null,
608
673
  firstName: firstName || null,
609
674
  lastName: lastName || null,
675
+ // FIXME: Double check this
610
676
  id: this.context.user.id,
611
677
  accountId: this.context.user.accountId
612
678
  };
@@ -628,6 +694,13 @@ class Widget {
628
694
  widgetType: this.type,
629
695
  jwt
630
696
  });
697
+ } else if (this.context.type === "passwordless") {
698
+ response = this.widgetApi.render({
699
+ user: undefined,
700
+ engagementMedium,
701
+ widgetType: this.type,
702
+ jwt: undefined
703
+ });
631
704
  } else {
632
705
  throw new Error("can't reload an error widget");
633
706
  }
@@ -662,7 +735,7 @@ class Widget {
662
735
  }).catch(({
663
736
  message
664
737
  }) => {
665
- _log$6(`${message}`);
738
+ _log$7(`${message}`);
666
739
  });
667
740
  }
668
741
 
@@ -699,7 +772,7 @@ function domready(targetDoc, fn) {
699
772
 
700
773
  // @ts-check
701
774
 
702
- const _log$5 = debug__default("squatch-js:EMBEDwidget");
775
+ const _log$6 = debug__default("squatch-js:EMBEDwidget");
703
776
  /**
704
777
  * An EmbedWidget is displayed inline in part of your page.
705
778
  *
@@ -717,22 +790,22 @@ class EmbedWidget extends Widget {
717
790
  // selector is a string
718
791
  element = document.querySelector(container);
719
792
 
720
- _log$5("loading widget with selector", element); // selector is an HTML element
793
+ _log$6("loading widget with selector", element); // selector is an HTML element
721
794
 
722
795
  } else if (container instanceof HTMLElement) {
723
796
  element = container;
724
797
 
725
- _log$5("loading widget with container", element); // garbage container found
798
+ _log$6("loading widget with container", element); // garbage container found
726
799
 
727
800
  } else if (container) {
728
801
  element = null;
729
802
 
730
- _log$5("container must be an HTMLElement or string", container); // find element on page
803
+ _log$6("container must be an HTMLElement or string", container); // find element on page
731
804
 
732
805
  } else {
733
806
  element = document.querySelector("#squatchembed") || document.querySelector(".squatchembed");
734
807
 
735
- _log$5("loading widget with default selector", element);
808
+ _log$6("loading widget with default selector", element);
736
809
  }
737
810
 
738
811
  if (!(element instanceof HTMLElement)) throw new Error(`element with selector '${container}' not found.'`);
@@ -798,7 +871,7 @@ class EmbedWidget extends Widget {
798
871
  if (!this.context.container) {
799
872
  this._loadEvent(_sqh);
800
873
 
801
- _log$5("loaded");
874
+ _log$6("loaded");
802
875
  }
803
876
  });
804
877
  } // Un-hide if element is available and refresh data
@@ -807,7 +880,7 @@ class EmbedWidget extends Widget {
807
880
  open() {
808
881
  var _this$frame, _this$frame$contentDo, _this$frame2, _this$frame2$contentW, _this$frame3, _this$frame3$contentW;
809
882
 
810
- if (!this.frame) return _log$5("no target element to open");
883
+ if (!this.frame) return _log$6("no target element to open");
811
884
  this.element.style.visibility = "unset";
812
885
  this.element.style.height = "auto";
813
886
  this.element.style["overflow-y"] = "auto";
@@ -817,16 +890,16 @@ class EmbedWidget extends Widget {
817
890
 
818
891
  this._loadEvent(_sqh);
819
892
 
820
- _log$5("loaded");
893
+ _log$6("loaded");
821
894
  }
822
895
 
823
896
  close() {
824
- if (!this.frame) return _log$5("no target element to close");
897
+ if (!this.frame) return _log$6("no target element to close");
825
898
  this.element.style.visibility = "hidden";
826
899
  this.element.style.height = "0";
827
900
  this.element.style["overflow-y"] = "hidden";
828
901
 
829
- _log$5("Embed widget closed");
902
+ _log$6("Embed widget closed");
830
903
  }
831
904
 
832
905
  _error(rs, mode = "embed", style = "") {
@@ -837,7 +910,7 @@ class EmbedWidget extends Widget {
837
910
 
838
911
  // @ts-check
839
912
 
840
- const _log$4 = debug__default("squatch-js:POPUPwidget");
913
+ const _log$5 = debug__default("squatch-js:POPUPwidget");
841
914
  /**
842
915
  * The PopupWidget is used to display popups (also known as "Modals").
843
916
  * Popups widgets are rendered on top of other elements in a page.
@@ -855,9 +928,9 @@ class PopupWidget extends Widget {
855
928
  this.triggerElement
856
929
  /* HTMLButton */
857
930
  = document.querySelector(trigger);
858
- if (trigger && !this.triggerElement) _log$4("No element found with trigger selector", trigger);
931
+ if (trigger && !this.triggerElement) _log$5("No element found with trigger selector", trigger);
859
932
  } catch (_unused) {
860
- _log$4("Not a valid selector", trigger);
933
+ _log$5("Not a valid selector", trigger);
861
934
  } // Trigger is optional
862
935
 
863
936
 
@@ -899,7 +972,7 @@ class PopupWidget extends Widget {
899
972
  frameDoc.write(`<script src="${this.npmCdn}/resize-observer-polyfill@1.5.x"></script>`);
900
973
  frameDoc.close();
901
974
 
902
- _log$4("Popup template loaded into iframe");
975
+ _log$5("Popup template loaded into iframe");
903
976
 
904
977
  this._setupResizeHandler();
905
978
  }
@@ -972,7 +1045,7 @@ class PopupWidget extends Widget {
972
1045
 
973
1046
  this._loadEvent(_sqh);
974
1047
 
975
- _log$4("Popup opened");
1048
+ _log$5("Popup opened");
976
1049
  });
977
1050
  }
978
1051
 
@@ -980,7 +1053,7 @@ class PopupWidget extends Widget {
980
1053
  this.popupdiv.style.visibility = "hidden";
981
1054
  this.popupdiv.style.top = "-2000px";
982
1055
 
983
- _log$4("Popup closed");
1056
+ _log$5("Popup closed");
984
1057
  }
985
1058
 
986
1059
  _clickedOutside({
@@ -998,7 +1071,7 @@ class PopupWidget extends Widget {
998
1071
 
999
1072
  }
1000
1073
 
1001
- const _log$3 = debug("squatch-js:CTAwidget");
1074
+ const _log$4 = debug("squatch-js:CTAwidget");
1002
1075
  /**
1003
1076
  * A CtaWidget is displayed on top of your page
1004
1077
  *
@@ -1009,7 +1082,7 @@ const _log$3 = debug("squatch-js:CTAwidget");
1009
1082
 
1010
1083
  class CtaWidget extends PopupWidget {
1011
1084
  constructor(params, opts) {
1012
- _log$3("CTA constructor");
1085
+ _log$4("CTA constructor");
1013
1086
 
1014
1087
  const ctaElement = document.createElement("div");
1015
1088
  ctaElement.id = "cta";
@@ -1037,7 +1110,7 @@ class CtaWidget extends PopupWidget {
1037
1110
  this.ctaFrame.setAttribute("style", `border:0; background-color:transparent; position:fixed; display:none;${this.side}${this.position}`);
1038
1111
  document.body.appendChild(this.ctaFrame);
1039
1112
 
1040
- _log$3("ctaframe appended to body");
1113
+ _log$4("ctaframe appended to body");
1041
1114
  }
1042
1115
 
1043
1116
  load() {
@@ -1101,10 +1174,10 @@ class CtaWidget extends PopupWidget {
1101
1174
  });
1102
1175
  ro.observe(ctaContainer);
1103
1176
 
1104
- _log$3("CTA template loaded into iframe");
1177
+ _log$4("CTA template loaded into iframe");
1105
1178
  });
1106
1179
  } else {
1107
- _log$3(new Error("CTA element not found in theme"));
1180
+ _log$4(new Error("CTA element not found in theme"));
1108
1181
  }
1109
1182
  });
1110
1183
  }
@@ -1127,7 +1200,7 @@ class CtaWidget extends PopupWidget {
1127
1200
 
1128
1201
  }
1129
1202
 
1130
- const _log$2 = debug__default("squatch-js:widgets");
1203
+ const _log$3 = debug__default("squatch-js:widgets");
1131
1204
  /**
1132
1205
  *
1133
1206
  * `Widgets` is a factory for creating widgets. It's possible to build your own widgets using the
@@ -1191,7 +1264,7 @@ class Widgets {
1191
1264
  user: response.user
1192
1265
  };
1193
1266
  } catch (err) {
1194
- _log$2(err);
1267
+ _log$3(err);
1195
1268
 
1196
1269
  if (err.apiErrorCode) {
1197
1270
  this._renderErrorWidget(err, config.engagementMedium);
@@ -1227,7 +1300,7 @@ class Widgets {
1227
1300
  return {
1228
1301
  widget: this._renderWidget(response, clean, {
1229
1302
  type: "upsert",
1230
- user: clean.user,
1303
+ user: clean.user || null,
1231
1304
  engagementMedium: config.engagementMedium,
1232
1305
  container: config.container,
1233
1306
  trigger: config.trigger
@@ -1235,7 +1308,7 @@ class Widgets {
1235
1308
  user: response.user
1236
1309
  };
1237
1310
  } catch (err) {
1238
- _log$2(err);
1311
+ _log$3(err);
1239
1312
 
1240
1313
  if (err.apiErrorCode) {
1241
1314
  this._renderErrorWidget(err, config.engagementMedium);
@@ -1263,19 +1336,26 @@ class Widgets {
1263
1336
 
1264
1337
  async render(config) {
1265
1338
  const raw = config;
1266
- const clean = validateWidgetConfig(raw);
1339
+ const clean = validatePasswordlessConfig(raw);
1267
1340
 
1268
1341
  try {
1269
- const response = await this.api.cookieUser(clean);
1270
- return {
1271
- widget: this._renderWidget({
1272
- template: response
1273
- }, clean, {
1274
- type: "cookie",
1275
- engagementMedium: clean.engagementMedium
1276
- }),
1277
- user: response.user
1278
- };
1342
+ // TODO: Flagging default behaviour change
1343
+ // cookieUser returns a deprecated error from the API on the latest squatchJs version
1344
+ // More suitable for no auth render?
1345
+ const queryString = window.location.search;
1346
+ const urlParams = new URLSearchParams(queryString);
1347
+ const refParam = urlParams.get("_saasquatch") || "";
1348
+
1349
+ if (!config.showOnReferral || refParam) {
1350
+ const response = await this.api.render(clean);
1351
+ return {
1352
+ widget: this._renderWidget(response, clean, {
1353
+ type: "passwordless",
1354
+ engagementMedium: clean.engagementMedium
1355
+ }),
1356
+ user: response.user
1357
+ };
1358
+ }
1279
1359
  } catch (err) {
1280
1360
  if (err.apiErrorCode) {
1281
1361
  this._renderErrorWidget(err, clean.engagementMedium);
@@ -1298,7 +1378,7 @@ class Widgets {
1298
1378
 
1299
1379
  if (typeof input === "function") {
1300
1380
  this.api.squatchReferralCookie().then((...args) => input(...args)).catch(ex => {
1301
- _log$2("Autofill error", ex);
1381
+ _log$3("Autofill error", ex);
1302
1382
 
1303
1383
  throw ex;
1304
1384
  });
@@ -1313,7 +1393,7 @@ class Widgets {
1313
1393
  // Only use the first element found
1314
1394
  elem = elems[0];
1315
1395
  } else {
1316
- _log$2("Element id/class or function missing");
1396
+ _log$3("Element id/class or function missing");
1317
1397
 
1318
1398
  throw new Error("Element id/class or function missing");
1319
1399
  }
@@ -1351,7 +1431,7 @@ class Widgets {
1351
1431
 
1352
1432
 
1353
1433
  _renderWidget(response, config, context) {
1354
- _log$2("Rendering Widget...");
1434
+ _log$3("Rendering Widget...");
1355
1435
 
1356
1436
  if (!response) throw new Error("Unable to get a response");
1357
1437
  let widget;
@@ -1374,9 +1454,9 @@ class Widgets {
1374
1454
  displayOnLoad = rule.displayOnLoad;
1375
1455
  displayCTA = rule.showAsCTA;
1376
1456
 
1377
- _log$2(`Display ${rule.widgetType} on ${rule.url}`);
1457
+ _log$3(`Display ${rule.widgetType} on ${rule.url}`);
1378
1458
  } else {
1379
- _log$2(`Don't display ${rule.widgetType} when no referral on widget rule match ${rule.url}`);
1459
+ _log$3(`Don't display ${rule.widgetType} when no referral on widget rule match ${rule.url}`);
1380
1460
  }
1381
1461
  }
1382
1462
  });
@@ -1385,20 +1465,20 @@ class Widgets {
1385
1465
  if (opts.conversionUrls) {
1386
1466
  opts.conversionUrls.forEach(rule => {
1387
1467
  if (response.user.referredBy && Widgets._matchesUrl(rule)) {
1388
- _log$2("This is a conversion URL", rule);
1468
+ _log$3("This is a conversion URL", rule);
1389
1469
  }
1390
1470
  });
1391
1471
  }
1392
1472
 
1393
1473
  if (opts.fuelTankAutofillUrls) {
1394
- _log$2("We found a fuel tank autofill!");
1474
+ _log$3("We found a fuel tank autofill!");
1395
1475
 
1396
1476
  opts.fuelTankAutofillUrls.forEach(({
1397
1477
  url,
1398
1478
  formSelector
1399
1479
  }) => {
1400
1480
  if (Widgets._matchesUrl(url)) {
1401
- _log$2("Fuel Tank URL matches");
1481
+ _log$3("Fuel Tank URL matches");
1402
1482
 
1403
1483
  if (response.user.referredBy && response.user.referredBy.code) {
1404
1484
  const formAutofill = document.querySelector(formSelector);
@@ -1406,7 +1486,7 @@ class Widgets {
1406
1486
  if (formAutofill) {
1407
1487
  formAutofill.value = response.user.referredBy.referredReward.fuelTankCode || "";
1408
1488
  } else {
1409
- _log$2(new Error(`Element with id/class ${formSelector} was not found.`));
1489
+ _log$3(new Error(`Element with id/class ${formSelector} was not found.`));
1410
1490
  }
1411
1491
  }
1412
1492
  }
@@ -1421,7 +1501,7 @@ class Widgets {
1421
1501
  widget.load();
1422
1502
  if (displayOnLoad) widget.open();
1423
1503
  } else if (displayCTA) {
1424
- _log$2("display CTA");
1504
+ _log$3("display CTA");
1425
1505
 
1426
1506
  const side = opts.cta.content.buttonSide;
1427
1507
  const position = opts.cta.content.buttonPosition;
@@ -1432,7 +1512,7 @@ class Widgets {
1432
1512
  widget.load();
1433
1513
  if (displayOnLoad) widget.open();
1434
1514
  } else {
1435
- _log$2("display popup on load");
1515
+ _log$3("display popup on load");
1436
1516
 
1437
1517
  widget = new PopupWidget(params);
1438
1518
  widget.load();
@@ -1456,7 +1536,7 @@ class Widgets {
1456
1536
  message
1457
1537
  } = props;
1458
1538
 
1459
- _log$2(new Error(`${apiErrorCode} (${rsCode}) ${message}`));
1539
+ _log$3(new Error(`${apiErrorCode} (${rsCode}) ${message}`));
1460
1540
 
1461
1541
  const params = {
1462
1542
  content: "error",
@@ -1986,7 +2066,7 @@ var URLSearchParams$1 = self.URLSearchParams;
1986
2066
 
1987
2067
  /** @hidden */
1988
2068
 
1989
- const _log$1 = debug__default("squatch-js");
2069
+ const _log$2 = debug__default("squatch-js");
1990
2070
 
1991
2071
  const isObject = item => typeof item === "object" && !Array.isArray(item);
1992
2072
 
@@ -2039,7 +2119,7 @@ function _pushCookie() {
2039
2119
  try {
2040
2120
  paramsJSON = JSON.parse(b64decode(refParam));
2041
2121
  } catch (error) {
2042
- _log$1("Unable to decode params", error); // don't merge invalid params
2122
+ _log$2("Unable to decode params", error); // don't merge invalid params
2043
2123
 
2044
2124
 
2045
2125
  return;
@@ -2048,26 +2128,26 @@ function _pushCookie() {
2048
2128
  try {
2049
2129
  existingCookie = JSON.parse(b64decode(Cookies.get("_saasquatch")));
2050
2130
 
2051
- _log$1("existing cookie", existingCookie);
2131
+ _log$2("existing cookie", existingCookie);
2052
2132
  } catch (error) {
2053
- _log$1("Unable to retrieve cookie", error);
2133
+ _log$2("Unable to retrieve cookie", error);
2054
2134
  } // don't merge if there's no existing object
2055
2135
 
2056
2136
 
2057
2137
  try {
2058
2138
  const domain = getTopDomain();
2059
2139
 
2060
- _log$1("domain retrieved:", domain);
2140
+ _log$2("domain retrieved:", domain);
2061
2141
 
2062
2142
  if (existingCookie) {
2063
2143
  const newCookie = deepMerge(existingCookie, paramsJSON);
2064
2144
  reEncodedCookie = b64encode(JSON.stringify(newCookie));
2065
2145
 
2066
- _log$1("cookie to store:", newCookie);
2146
+ _log$2("cookie to store:", newCookie);
2067
2147
  } else {
2068
2148
  reEncodedCookie = b64encode(JSON.stringify(paramsJSON));
2069
2149
 
2070
- _log$1("cookie to store:", paramsJSON);
2150
+ _log$2("cookie to store:", paramsJSON);
2071
2151
  }
2072
2152
 
2073
2153
  Cookies.set("_saasquatch", reEncodedCookie, {
@@ -2078,11 +2158,83 @@ function _pushCookie() {
2078
2158
  path: "/"
2079
2159
  });
2080
2160
  } catch (error) {
2081
- _log$1("Unable to set cookie", error);
2161
+ _log$2("Unable to set cookie", error);
2082
2162
  }
2083
2163
  }
2084
2164
  }
2085
2165
 
2166
+ /** @hidden */
2167
+
2168
+ const _log$1 = debug__default("squatch-js");
2169
+
2170
+ function _getConfig(configIn) {
2171
+ const queryString = window.location.search;
2172
+ const urlParams = new URLSearchParams(queryString);
2173
+ const refParam = urlParams.get("_saasquatchExtra") || "";
2174
+
2175
+ if (!refParam) {
2176
+ _log$1("No _saasquatchExtra param");
2177
+
2178
+ return;
2179
+ }
2180
+
2181
+ let raw;
2182
+
2183
+ try {
2184
+ raw = JSON.parse(b64decode(refParam));
2185
+ } catch (e) {
2186
+ _log$1("Unable to decode _saasquatchExtra config");
2187
+
2188
+ return;
2189
+ }
2190
+
2191
+ const {
2192
+ domain,
2193
+ tenantAlias,
2194
+ widgetConfig
2195
+ } = convertExtraToConfig(raw);
2196
+
2197
+ if (!domain || !tenantAlias || !widgetConfig) {
2198
+ _log$1("_saasquatchExtra did not have an expected structure");
2199
+
2200
+ return undefined;
2201
+ }
2202
+
2203
+ const {
2204
+ autoPopupWidgetType
2205
+ } = widgetConfig,
2206
+ rest = _objectWithoutPropertiesLoose(widgetConfig, ["autoPopupWidgetType"]);
2207
+
2208
+ return {
2209
+ widgetConfig: _extends({
2210
+ widgetType: autoPopupWidgetType
2211
+ }, rest),
2212
+ squatchConfig: _extends({}, configIn ? {
2213
+ configIn
2214
+ } : {}, {
2215
+ domain,
2216
+ tenantAlias
2217
+ })
2218
+ };
2219
+ }
2220
+ /**
2221
+ * Converts _saasquatchExtra into
2222
+ * @param obj
2223
+ */
2224
+
2225
+ function convertExtraToConfig(obj) {
2226
+ var _obj$domain;
2227
+
2228
+ const domain = Object.keys(obj || {})[0];
2229
+ const tenantAlias = Object.keys((obj == null ? void 0 : obj[domain]) || {})[0];
2230
+ const widgetConfig = obj == null ? void 0 : (_obj$domain = obj[domain]) == null ? void 0 : _obj$domain[tenantAlias];
2231
+ return {
2232
+ domain,
2233
+ tenantAlias,
2234
+ widgetConfig
2235
+ };
2236
+ }
2237
+
2086
2238
  // @ts-check
2087
2239
  function help() {
2088
2240
  console.log(`Having trouble using Squatch.js? Go to https://docs.referralsaasquatch.com/developer/ for tutorials, references and error codes.`);
@@ -2136,6 +2288,35 @@ function widgets() {
2136
2288
  function events() {
2137
2289
  return _events;
2138
2290
  }
2291
+ /**
2292
+ * Entry-point for high level API to render a widget using the instance of {@link Widgets} created when you call {@link #init init}.
2293
+ */
2294
+
2295
+ function widget(widgetConfig) {
2296
+ var _widgets2;
2297
+
2298
+ return (_widgets2 = widgets()) == null ? void 0 : _widgets2.render(widgetConfig);
2299
+ }
2300
+ /**
2301
+ * Initial concept for automatic widget rendering
2302
+ *
2303
+ * - `saasquatchExtra` utm param carries widgetIdent
2304
+ */
2305
+
2306
+ function auto(configIn) {
2307
+ const configs = _getConfig(configIn);
2308
+
2309
+ if (configs) {
2310
+ var _widgets3;
2311
+
2312
+ const {
2313
+ squatchConfig,
2314
+ widgetConfig
2315
+ } = configs;
2316
+ init(squatchConfig);
2317
+ return widgetConfig && ((_widgets3 = widgets()) == null ? void 0 : _widgets3.render(widgetConfig));
2318
+ }
2319
+ }
2139
2320
  /**
2140
2321
  * Initializes the static `squatch` global. This sets up:
2141
2322
  *
@@ -2240,5 +2421,5 @@ if (typeof document !== "undefined" && !window.SaaSquatchDoNotAutoDrop) {
2240
2421
 
2241
2422
  if (typeof document !== "undefined") asyncLoad();
2242
2423
 
2243
- export { CtaWidget, EmbedWidget, PopupWidget, WidgetApi, Widgets, api, autofill, events, help, init, pushCookie, ready, submitEmail, widgets };
2424
+ export { CtaWidget, EmbedWidget, PopupWidget, WidgetApi, Widgets, api, auto, autofill, events, help, init, pushCookie, ready, submitEmail, widget, widgets };
2244
2425
  //# sourceMappingURL=squatch.esm.js.map