@saasquatch/squatch-js 2.4.2 → 2.4.3-1

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
  }
@@ -1346,16 +1426,17 @@ class Widgets {
1346
1426
  * @param {Object} config Config details
1347
1427
  * @param {string} config.widgetType The widget type (REFERRER_WIDGET, CONVERSION_WIDGET)
1348
1428
  * @param {string} config.engagementMedium (POPUP, EMBED)
1429
+ * @param {string} config.showOnReferral Whether to show
1349
1430
  * @returns {Widget} widget (PopupWidget, EmbedWidget, or CtaWidget)
1350
1431
  */
1351
1432
 
1352
1433
 
1353
1434
  _renderWidget(response, config, context) {
1354
- _log$2("Rendering Widget...");
1435
+ _log$3("Rendering Widget...");
1355
1436
 
1356
1437
  if (!response) throw new Error("Unable to get a response");
1357
1438
  let widget;
1358
- let displayOnLoad = false;
1439
+ let displayOnLoad = !!config.showOnReferral;
1359
1440
  let displayCTA = false;
1360
1441
  const opts = response.jsOptions || "";
1361
1442
  const params = {
@@ -1374,9 +1455,9 @@ class Widgets {
1374
1455
  displayOnLoad = rule.displayOnLoad;
1375
1456
  displayCTA = rule.showAsCTA;
1376
1457
 
1377
- _log$2(`Display ${rule.widgetType} on ${rule.url}`);
1458
+ _log$3(`Display ${rule.widgetType} on ${rule.url}`);
1378
1459
  } else {
1379
- _log$2(`Don't display ${rule.widgetType} when no referral on widget rule match ${rule.url}`);
1460
+ _log$3(`Don't display ${rule.widgetType} when no referral on widget rule match ${rule.url}`);
1380
1461
  }
1381
1462
  }
1382
1463
  });
@@ -1385,20 +1466,20 @@ class Widgets {
1385
1466
  if (opts.conversionUrls) {
1386
1467
  opts.conversionUrls.forEach(rule => {
1387
1468
  if (response.user.referredBy && Widgets._matchesUrl(rule)) {
1388
- _log$2("This is a conversion URL", rule);
1469
+ _log$3("This is a conversion URL", rule);
1389
1470
  }
1390
1471
  });
1391
1472
  }
1392
1473
 
1393
1474
  if (opts.fuelTankAutofillUrls) {
1394
- _log$2("We found a fuel tank autofill!");
1475
+ _log$3("We found a fuel tank autofill!");
1395
1476
 
1396
1477
  opts.fuelTankAutofillUrls.forEach(({
1397
1478
  url,
1398
1479
  formSelector
1399
1480
  }) => {
1400
1481
  if (Widgets._matchesUrl(url)) {
1401
- _log$2("Fuel Tank URL matches");
1482
+ _log$3("Fuel Tank URL matches");
1402
1483
 
1403
1484
  if (response.user.referredBy && response.user.referredBy.code) {
1404
1485
  const formAutofill = document.querySelector(formSelector);
@@ -1406,7 +1487,7 @@ class Widgets {
1406
1487
  if (formAutofill) {
1407
1488
  formAutofill.value = response.user.referredBy.referredReward.fuelTankCode || "";
1408
1489
  } else {
1409
- _log$2(new Error(`Element with id/class ${formSelector} was not found.`));
1490
+ _log$3(new Error(`Element with id/class ${formSelector} was not found.`));
1410
1491
  }
1411
1492
  }
1412
1493
  }
@@ -1421,7 +1502,7 @@ class Widgets {
1421
1502
  widget.load();
1422
1503
  if (displayOnLoad) widget.open();
1423
1504
  } else if (displayCTA) {
1424
- _log$2("display CTA");
1505
+ _log$3("display CTA");
1425
1506
 
1426
1507
  const side = opts.cta.content.buttonSide;
1427
1508
  const position = opts.cta.content.buttonPosition;
@@ -1432,7 +1513,7 @@ class Widgets {
1432
1513
  widget.load();
1433
1514
  if (displayOnLoad) widget.open();
1434
1515
  } else {
1435
- _log$2("display popup on load");
1516
+ _log$3("display popup on load");
1436
1517
 
1437
1518
  widget = new PopupWidget(params);
1438
1519
  widget.load();
@@ -1456,7 +1537,7 @@ class Widgets {
1456
1537
  message
1457
1538
  } = props;
1458
1539
 
1459
- _log$2(new Error(`${apiErrorCode} (${rsCode}) ${message}`));
1540
+ _log$3(new Error(`${apiErrorCode} (${rsCode}) ${message}`));
1460
1541
 
1461
1542
  const params = {
1462
1543
  content: "error",
@@ -1986,7 +2067,7 @@ var URLSearchParams$1 = self.URLSearchParams;
1986
2067
 
1987
2068
  /** @hidden */
1988
2069
 
1989
- const _log$1 = debug__default("squatch-js");
2070
+ const _log$2 = debug__default("squatch-js");
1990
2071
 
1991
2072
  const isObject = item => typeof item === "object" && !Array.isArray(item);
1992
2073
 
@@ -2039,7 +2120,7 @@ function _pushCookie() {
2039
2120
  try {
2040
2121
  paramsJSON = JSON.parse(b64decode(refParam));
2041
2122
  } catch (error) {
2042
- _log$1("Unable to decode params", error); // don't merge invalid params
2123
+ _log$2("Unable to decode params", error); // don't merge invalid params
2043
2124
 
2044
2125
 
2045
2126
  return;
@@ -2048,26 +2129,26 @@ function _pushCookie() {
2048
2129
  try {
2049
2130
  existingCookie = JSON.parse(b64decode(Cookies.get("_saasquatch")));
2050
2131
 
2051
- _log$1("existing cookie", existingCookie);
2132
+ _log$2("existing cookie", existingCookie);
2052
2133
  } catch (error) {
2053
- _log$1("Unable to retrieve cookie", error);
2134
+ _log$2("Unable to retrieve cookie", error);
2054
2135
  } // don't merge if there's no existing object
2055
2136
 
2056
2137
 
2057
2138
  try {
2058
2139
  const domain = getTopDomain();
2059
2140
 
2060
- _log$1("domain retrieved:", domain);
2141
+ _log$2("domain retrieved:", domain);
2061
2142
 
2062
2143
  if (existingCookie) {
2063
2144
  const newCookie = deepMerge(existingCookie, paramsJSON);
2064
2145
  reEncodedCookie = b64encode(JSON.stringify(newCookie));
2065
2146
 
2066
- _log$1("cookie to store:", newCookie);
2147
+ _log$2("cookie to store:", newCookie);
2067
2148
  } else {
2068
2149
  reEncodedCookie = b64encode(JSON.stringify(paramsJSON));
2069
2150
 
2070
- _log$1("cookie to store:", paramsJSON);
2151
+ _log$2("cookie to store:", paramsJSON);
2071
2152
  }
2072
2153
 
2073
2154
  Cookies.set("_saasquatch", reEncodedCookie, {
@@ -2078,17 +2159,90 @@ function _pushCookie() {
2078
2159
  path: "/"
2079
2160
  });
2080
2161
  } catch (error) {
2081
- _log$1("Unable to set cookie", error);
2162
+ _log$2("Unable to set cookie", error);
2082
2163
  }
2083
2164
  }
2084
2165
  }
2085
2166
 
2167
+ /** @hidden */
2168
+
2169
+ const _log$1 = debug__default("squatch-js");
2170
+
2171
+ function _getAutoConfig(configIn) {
2172
+ const queryString = window.location.search;
2173
+ const urlParams = new URLSearchParams(queryString);
2174
+ const refParam = urlParams.get("_saasquatchExtra") || "";
2175
+
2176
+ if (!refParam) {
2177
+ _log$1("No _saasquatchExtra param");
2178
+
2179
+ return;
2180
+ }
2181
+
2182
+ let raw;
2183
+
2184
+ try {
2185
+ raw = JSON.parse(b64decode(refParam));
2186
+ } catch (e) {
2187
+ _log$1("Unable to decode _saasquatchExtra config");
2188
+
2189
+ return;
2190
+ }
2191
+
2192
+ const {
2193
+ domain,
2194
+ tenantAlias,
2195
+ widgetConfig
2196
+ } = convertExtraToConfig(raw);
2197
+
2198
+ if (!domain || !tenantAlias || !widgetConfig) {
2199
+ _log$1("_saasquatchExtra did not have an expected structure");
2200
+
2201
+ return undefined;
2202
+ }
2203
+
2204
+ const {
2205
+ autoPopupWidgetType
2206
+ } = widgetConfig,
2207
+ rest = _objectWithoutPropertiesLoose(widgetConfig, ["autoPopupWidgetType"]);
2208
+
2209
+ return {
2210
+ widgetConfig: _extends({
2211
+ widgetType: autoPopupWidgetType
2212
+ }, rest),
2213
+ squatchConfig: _extends({}, configIn ? {
2214
+ configIn
2215
+ } : {}, {
2216
+ domain,
2217
+ tenantAlias
2218
+ })
2219
+ };
2220
+ }
2221
+ /**
2222
+ * Converts _saasquatchExtra into
2223
+ * @param obj
2224
+ */
2225
+
2226
+ function convertExtraToConfig(obj) {
2227
+ var _obj$_domain;
2228
+
2229
+ const _domain = Object.keys(obj || {})[0];
2230
+ const tenantAlias = Object.keys((obj == null ? void 0 : obj[_domain]) || {})[0];
2231
+ const widgetConfig = obj == null ? void 0 : (_obj$_domain = obj[_domain]) == null ? void 0 : _obj$_domain[tenantAlias]; // domain in _saasquatchExtra doesn't contain "https://"
2232
+
2233
+ const domain = _domain ? `https://${_domain}` : undefined;
2234
+ return {
2235
+ domain,
2236
+ tenantAlias,
2237
+ widgetConfig
2238
+ };
2239
+ }
2240
+
2086
2241
  // @ts-check
2087
2242
  function help() {
2088
2243
  console.log(`Having trouble using Squatch.js? Go to https://docs.referralsaasquatch.com/developer/ for tutorials, references and error codes.`);
2089
2244
  }
2090
2245
 
2091
- // @ts-check
2092
2246
  // debug.disable("squatch-js*");
2093
2247
 
2094
2248
  /** @hidden */
@@ -2136,6 +2290,37 @@ function widgets() {
2136
2290
  function events() {
2137
2291
  return _events;
2138
2292
  }
2293
+ /**
2294
+ * Entry-point for high level API to render a widget using the instance of {@link Widgets} created when you call {@link #init init}.
2295
+ */
2296
+
2297
+ function widget(widgetConfig) {
2298
+ var _widgets2;
2299
+
2300
+ return (_widgets2 = widgets()) == null ? void 0 : _widgets2.render(widgetConfig);
2301
+ }
2302
+ /**
2303
+ * Initial concept for automatic widget rendering
2304
+ *
2305
+ * - `saasquatchExtra` utm param carries widgetIdent
2306
+ */
2307
+
2308
+ function auto(configIn) {
2309
+ const configs = _getAutoConfig(configIn);
2310
+
2311
+ if (configs) {
2312
+ var _widgets3;
2313
+
2314
+ const {
2315
+ squatchConfig,
2316
+ widgetConfig
2317
+ } = configs;
2318
+ init(squatchConfig);
2319
+ return (_widgets3 = widgets()) == null ? void 0 : _widgets3.render(_extends({
2320
+ showOnReferral: true
2321
+ }, widgetConfig));
2322
+ }
2323
+ }
2139
2324
  /**
2140
2325
  * Initializes the static `squatch` global. This sets up:
2141
2326
  *
@@ -2240,5 +2425,5 @@ if (typeof document !== "undefined" && !window.SaaSquatchDoNotAutoDrop) {
2240
2425
 
2241
2426
  if (typeof document !== "undefined") asyncLoad();
2242
2427
 
2243
- export { CtaWidget, EmbedWidget, PopupWidget, WidgetApi, Widgets, api, autofill, events, help, init, pushCookie, ready, submitEmail, widgets };
2428
+ export { CtaWidget, EmbedWidget, PopupWidget, WidgetApi, Widgets, api, auto, autofill, events, help, init, pushCookie, ready, submitEmail, widget, widgets };
2244
2429
  //# sourceMappingURL=squatch.esm.js.map