sfc-utils 1.4.91 → 1.4.93

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/personalize.js +56 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfc-utils",
3
- "version": "1.4.91",
3
+ "version": "1.4.93",
4
4
  "author": "ewagstaff <evanjwagstaff@gmail.com>",
5
5
  "dependencies": {
6
6
  "archieml": "^0.4.2",
package/personalize.js CHANGED
@@ -2,34 +2,71 @@
2
2
  const getProfileProperty = (property) => {
3
3
  return new Promise((resolve, reject) => {
4
4
  if (window) {
5
- if (window.blueConicClient) {
6
- const profile = window.blueConicClient.profile.getProfile();
7
- const properties = [property];
8
- profile.loadValues(properties, this, function () {
9
- var value = profile.getValue(property);
10
- // Return a valid result or null
11
- value = value || null;
12
- console.log("getProfileProperty VAL", value);
13
- resolve(value);
14
- });
15
- } else {
16
- resolve("Personalize: No BlueConic client found");
17
- }
5
+ let attempts = 0;
6
+ const checkForBlueConic = (innerResolve) => {
7
+ if (window.blueConicClient) {
8
+ const profile = window.blueConicClient.profile.getProfile();
9
+ const properties = [property];
10
+ profile.loadValues(properties, this, function () {
11
+ var value = profile.getValue(property);
12
+ // Return a valid result or null
13
+ value = value || null;
14
+ console.log("getProfileProperty VAL", value);
15
+ innerResolve(value);
16
+ });
17
+ } else {
18
+ attempts++;
19
+ if (attempts < 20) {
20
+ // Check for BlueConic every second
21
+ setTimeout(() => {
22
+ checkForBlueConic(innerResolve);
23
+ }, 1000);
24
+ } else {
25
+ innerResolve(
26
+ "Personalize: No BlueConic client found after many attempts"
27
+ );
28
+ }
29
+ }
30
+ };
31
+ // Kick off check
32
+ checkForBlueConic(resolve);
18
33
  } else {
19
34
  resolve("Personalize: No window found");
20
35
  }
21
36
  });
22
37
  };
23
38
 
24
- const setProfileProperty = (property, value) => {
39
+ const setProfileProperty = (property, value, merge = false) => {
25
40
  return new Promise((resolve, reject) => {
26
41
  if (window) {
27
42
  if (window.blueConicClient) {
28
- const profile = window.blueConicClient.profile.getProfile();
29
- const properties = [property];
30
- profile.loadValues(properties, this, function () {
31
- profile.setValue(property, value);
32
- resolve(true);
43
+ // Prepare merging
44
+ let conditionalPromise = Promise.resolve();
45
+ if (merge && typeof value === "object") {
46
+ conditionalPromise = getProfileProperty(property);
47
+ }
48
+
49
+ conditionalPromise.then((existingValue) => {
50
+ // Finish merging
51
+ if (merge && typeof value === "object") {
52
+ console.log("Merging values...", existingValue, value);
53
+ value = JSON.stringify({ ...existingValue, ...value });
54
+ console.log("Merged value", value);
55
+ }
56
+ // Make sure property to save is a string
57
+ // If object, stringify it
58
+ if (typeof value === "object") {
59
+ value = JSON.stringify(value);
60
+ } else if (typeof value === "boolean" || typeof value === "number") {
61
+ value = value.toString();
62
+ }
63
+
64
+ const profile = window.blueConicClient.profile.getProfile();
65
+ const properties = [property];
66
+ profile.loadValues(properties, this, function () {
67
+ profile.setValue(property, value);
68
+ resolve(true);
69
+ });
33
70
  });
34
71
  } else {
35
72
  resolve("Personalize: No BlueConic client found");