dbm 1.1.3 → 1.1.4

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.
@@ -93,4 +93,12 @@ export const fullSetup = function() {
93
93
  currentDecoder.item.setValue("encodingType", name);
94
94
  currentDecoder.item.register(decodePrefix + name);
95
95
  }
96
+
97
+ {
98
+ let name = "visibility";
99
+ let currentDecoder = new Dbm.graphapi.webclient.decode.DecodeBaseObject();
100
+ currentDecoder.item.setValue("copyFields", ["visibility"]);
101
+ currentDecoder.item.setValue("encodingType", name);
102
+ currentDecoder.item.register(decodePrefix + name);
103
+ }
96
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbm",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {},
@@ -6,6 +6,7 @@ export default class Controller extends Dbm.core.BaseObject {
6
6
  super._construct();
7
7
 
8
8
  this.item.setValue("trackers", []);
9
+ this.item.setValue("currency", "EUR");
9
10
 
10
11
  Dbm.flow.addDirectUpdateCommand(this.item.requireProperty("active", false), Dbm.commands.callFunction(this._updateActiveStatus.bind(this)));
11
12
 
@@ -154,4 +155,84 @@ export default class Controller extends Dbm.core.BaseObject {
154
155
  }
155
156
  }
156
157
  }
158
+
159
+ _getValueFromItems(aItems) {
160
+ let value = 0;
161
+
162
+ let currentArray = aItems;
163
+ let currentArrayLength = currentArray.length;
164
+ for(let i = 0; i < currentArrayLength; i++) {
165
+ let currentItem = currentArray[i];
166
+
167
+ let quantity = currentItem.quantity ? currentItem.quantity : 1;
168
+ value += quantity*currentItem.price;
169
+ }
170
+
171
+ return value;
172
+ }
173
+
174
+ trackProductView(aProduct) {
175
+
176
+ let items = [aProduct];
177
+
178
+ let data = {
179
+ currency: this.item.currency,
180
+ value: this._getValueFromItems(items),
181
+ items: items
182
+ }
183
+
184
+ this.trackEvent("Product view", data);
185
+ }
186
+
187
+ trackAddedToCart(aProductOrProducts) {
188
+ let items = Dbm.utils.ArrayFunctions.singleOrArray(aProductOrProducts);
189
+
190
+ let data = {
191
+ currency: this.item.currency,
192
+ value: this._getValueFromItems(items),
193
+ items: items
194
+ }
195
+
196
+ this.trackEvent("Added to cart", data);
197
+
198
+ }
199
+
200
+ trackCheckoutStrated(aProductOrProducts) {
201
+ let items = Dbm.utils.ArrayFunctions.singleOrArray(aProductOrProducts);
202
+
203
+ let data = {
204
+ currency: this.item.currency,
205
+ value: this._getValueFromItems(items),
206
+ items: items
207
+ }
208
+
209
+ this.trackEvent("Checkout started", data);
210
+ }
211
+
212
+ trackPurchase(aTransactionId, aProductOrProducts) {
213
+ let items = Dbm.utils.ArrayFunctions.singleOrArray(aProductOrProducts);
214
+
215
+ let data = {
216
+ transaction_id: aTransactionId,
217
+ currency: this.item.currency,
218
+ value: this._getValueFromItems(items),
219
+ items: items
220
+ }
221
+
222
+ this.trackEvent("Purchase", data);
223
+ }
224
+
225
+ createProductItemData(aId, aName, aPrice, aQuantity = 1, aAddtionalData = {}) {
226
+
227
+ let returnObject = {
228
+ ...aAddtionalData,
229
+ id: aId,
230
+ item_id: aId,
231
+ item_name: aName,
232
+ price: aPrice,
233
+ quantity: aQuantity
234
+ }
235
+
236
+ return returnObject;
237
+ }
157
238
  }
@@ -1,7 +1,6 @@
1
1
  import Dbm from "../index.js";
2
- import Cookies from "js-cookie";
3
2
 
4
- export default class Controller extends Dbm.core.BaseObject {
3
+ export default class DataLayerTracker extends Dbm.core.BaseObject {
5
4
  _construct() {
6
5
  super._construct();
7
6
  }
@@ -68,6 +67,7 @@ export default class Controller extends Dbm.core.BaseObject {
68
67
  console.log(aEventName, aData);
69
68
 
70
69
  this.addToDataLayer({"event": "trackEvent", "value": {"name": aEventName, "data": aData}});
70
+ this._gtag("event", aEventName, aData);
71
71
  }
72
72
 
73
73
  trackCurrentPage() {
@@ -0,0 +1,112 @@
1
+ import Dbm from "../index.js";
2
+
3
+ export default class MetaPixelTracker extends Dbm.core.BaseObject {
4
+ _construct() {
5
+ super._construct();
6
+
7
+ this.item.requireProperty("pixelId")
8
+ }
9
+
10
+ _setupFunction() {
11
+
12
+ if(window.fbq) return;
13
+
14
+ let fbq = function() {
15
+ fbq.callMethod ? fbq.callMethod.apply(fbq, arguments) : fbq.queue.push(arguments);
16
+ }
17
+ window.fbq = fbq;
18
+
19
+ if(!window._fbq) {
20
+ window._fbq = fbq;
21
+ }
22
+
23
+ fbq.push = fbq;
24
+ fbq.loaded = true;
25
+ fbq.version = "2.0";
26
+ fbq.queue = [];
27
+ fbq.disablePushState = true;
28
+
29
+ return this;
30
+ }
31
+
32
+ startTracking() {
33
+
34
+ //MENOTE: do nothing
35
+
36
+ return this;
37
+ }
38
+
39
+ startStatisticsTracking() {
40
+
41
+ //MENOTE: do nothing
42
+
43
+ return this;
44
+ }
45
+
46
+ startMarketingTracking() {
47
+
48
+ this._setupFunction();
49
+ Dbm.loading.loadScript("https://connect.facebook.net/en_US/fbevents.js");
50
+
51
+ let pixelId = this.item.pixelId;
52
+
53
+ if(this.item.pixelId) {
54
+ window.fbq("init", this.item.pixelId);
55
+ window.fbq("track", "PageView");
56
+ }
57
+
58
+ return this;
59
+ }
60
+
61
+ stopTracking() {
62
+
63
+ return this;
64
+ }
65
+
66
+ _convertGooogleAnalyctisData(aData) {
67
+
68
+ let data = {
69
+ content_type: "product",
70
+ value: aData.value,
71
+ currency: aData.currency,
72
+ contents: aData.items,
73
+ num_items: aData.items.length,
74
+ content_ids: Dbm.utils.ArrayFunctions.mapField(aData.items, "id")
75
+ };
76
+
77
+ if(aData.items.length === 1) {
78
+ data["content_name"] = aData.items[0]["item_name"];
79
+ }
80
+
81
+ return data;
82
+ }
83
+
84
+ trackEvent(aEventName, aData) {
85
+ console.log("trackEvent");
86
+ console.log(aEventName, aData);
87
+
88
+ if(aEventName === "Purchase") {
89
+ window.fbq('track', 'Purchase', this._convertGooogleAnalyctisData(aData), {eventID: aData.transaction_id});
90
+ }
91
+ else if(aEventName === "Product view") {
92
+ window.fbq('track', 'ViewContent', this._convertGooogleAnalyctisData(aData));
93
+ }
94
+ else if(aEventName === "Added to cart") {
95
+ window.fbq('track', 'AddToCart', this._convertGooogleAnalyctisData(aData));
96
+ }
97
+ else if(aEventName === "Checkout started") {
98
+ window.fbq('track', 'InitiateCheckout', this._convertGooogleAnalyctisData(aData));
99
+ }
100
+ else {
101
+ window.fbq("trackCustom", aEventName, aData);
102
+ }
103
+ }
104
+
105
+ trackCurrentPage() {
106
+ window.fbq("track", "PageView");
107
+ }
108
+
109
+ trackPage(aUrl) {
110
+ window.fbq("track", "PageView");
111
+ }
112
+ }
package/tracking/index.js CHANGED
@@ -2,8 +2,9 @@ import Dbm from "../index.js";
2
2
 
3
3
  export {default as Controller} from "./Controller.js";
4
4
  export {default as DataLayerTracker} from "./DataLayerTracker.js";
5
+ export {default as MetaPixelTracker} from "./MetaPixelTracker.js";
5
6
 
6
- export let setup = function() {
7
+ export const setup = function() {
7
8
 
8
9
  let controller = new Dbm.tracking.Controller();
9
10
  controller.item.register("trackingController");
@@ -13,4 +14,10 @@ export let setup = function() {
13
14
 
14
15
  let dataLayerTracker = new Dbm.tracking.DataLayerTracker();
15
16
  controller.addTracker(dataLayerTracker.item);
17
+ }
18
+
19
+ export const addMetaPixel = function(aPixelId) {
20
+ let tracker = new Dbm.tracking.MetaPixelTracker();
21
+ tracker.item.pixelId = aPixelId;
22
+ Dbm.getInstance().repository.getItem("trackingController").controller.addTracker();
16
23
  }
@@ -130,5 +130,18 @@ export const filterByField = function(aArray, aField, aValue) {
130
130
  }
131
131
  }
132
132
 
133
+ return returnArray;
134
+ }
135
+
136
+ export const mapField = function(aArray, aField) {
137
+
138
+
139
+ let currentArray = aArray;
140
+ let currentArrayLength = currentArray.length;
141
+ let returnArray = new Array(acurrentArrayLength);
142
+ for(let i = 0; i < currentArrayLength; i++) {
143
+ returnArray[i] = Dbm.objectPath(aArray[i], aField);
144
+ }
145
+
133
146
  return returnArray;
134
147
  }