Browse Source

feat(analytics): Allow handler specific permanent props implementation.

dev1
Hristo Terezov 6 years ago
parent
commit
5cc705b9fd
1 changed files with 14 additions and 27 deletions
  1. 14
    27
      modules/statistics/AnalyticsAdapter.js

+ 14
- 27
modules/statistics/AnalyticsAdapter.js View File

121
         }
121
         }
122
 
122
 
123
         this.analyticsHandlers = new Set(handlers);
123
         this.analyticsHandlers = new Set(handlers);
124
+        this.analyticsHandlers.forEach(
125
+            handler => {
126
+                handler.setUserProperties(this.permanentProperties);
127
+            });
124
 
128
 
125
         // Note that we disable the cache even if the set of handlers is empty.
129
         // Note that we disable the cache even if the set of handlers is empty.
126
         const cache = this.cache;
130
         const cache = this.cache;
142
      * @param {Object} properties the properties to add
146
      * @param {Object} properties the properties to add
143
      */
147
      */
144
     addPermanentProperties(properties) {
148
     addPermanentProperties(properties) {
145
-        for (const property in properties) {
146
-            if (properties.hasOwnProperty(property)) {
147
-                this.permanentProperties[`permanent_${property}`]
148
-                    = properties[property];
149
-            }
150
-        }
149
+        this.permanentProperties = {
150
+            ...this.permanentProperties,
151
+            ...properties
152
+        };
153
+
154
+        this.analyticsHandlers.forEach(handler => {
155
+            handler.setUserProperties(this.permanentProperties);
156
+        });
151
     }
157
     }
152
 
158
 
153
     /**
159
     /**
305
         if (this._maybeCacheEvent(event)) {
311
         if (this._maybeCacheEvent(event)) {
306
             // The event was consumed by the cache.
312
             // The event was consumed by the cache.
307
         } else {
313
         } else {
308
-            // We append the permanent properties at the time we send the event,
309
-            // not at the time we receive it.
310
-            this._appendPermanentProperties(event);
311
-
312
-            for (const handler of this.analyticsHandlers) {
314
+            this.analyticsHandlers.forEach(handler => {
313
                 try {
315
                 try {
314
                     handler.sendEvent(event);
316
                     handler.sendEvent(event);
315
                 } catch (e) {
317
                 } catch (e) {
316
                     logger.warn(`Error sending analytics event: ${e}`);
318
                     logger.warn(`Error sending analytics event: ${e}`);
317
                 }
319
                 }
318
-            }
319
-        }
320
-    }
321
-
322
-    /**
323
-     * Extends an event object with the configured permanent properties.
324
-     * @param event the event to extend with permanent properties.
325
-     * @private
326
-     */
327
-    _appendPermanentProperties(event) {
328
-        if (!event.attributes) {
329
-            event.attributes = {};
320
+            });
330
         }
321
         }
331
-
332
-        event.attributes
333
-            = Object.assign(event.attributes, this.permanentProperties);
334
     }
322
     }
335
-
336
 }
323
 }
337
 
324
 
338
 export default new AnalyticsAdapter();
325
 export default new AnalyticsAdapter();

Loading…
Cancel
Save