You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2019/11/28 20:38:45 UTC

[unomi] 01/01: UNOMI-258 Add support for identify() and track() methods in web-tracker This commits add basic support to make sure that the identify and track methods do something and send back events to Unomi. This commit does not contain any documentation but it does add an example to the web tracker example page. You can view the generated events using the event-tail and event-view commands on the server.

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch UNOMI-258-identify-and-track-webtracker
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit a068e0242f50bc7f46a62e0020cf5ce3728c1396
Author: Serge Huber <sh...@apache.org>
AuthorDate: Thu Nov 28 21:38:37 2019 +0100

    UNOMI-258 Add support for identify() and track() methods in web-tracker
    This commits add basic support to make sure that the identify and track methods do something and send back events to Unomi.
    This commit does not contain any documentation but it does add an example to the web tracker example page.
    You can view the generated events using the event-tail and event-view commands on the server.
    
    Signed-off-by: Serge Huber <sh...@apache.org>
---
 .../web-tracker/javascript/dist/unomi-tracker.js      | 18 ++++++++++++++++--
 .../web-tracker/javascript/dist/unomi-tracker.min.js  |  2 +-
 .../src/analytics.js-integration-apache-unomi.js      | 19 +++++++++++++++----
 extensions/web-tracker/wab/src/main/webapp/index.html | 14 +++++++++++++-
 4 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/extensions/web-tracker/javascript/dist/unomi-tracker.js b/extensions/web-tracker/javascript/dist/unomi-tracker.js
index 34481a6..44ad76f 100644
--- a/extensions/web-tracker/javascript/dist/unomi-tracker.js
+++ b/extensions/web-tracker/javascript/dist/unomi-tracker.js
@@ -12684,7 +12684,9 @@ Unomi.prototype.processReferrer = function() {
 Unomi.prototype.onidentify = function(identify) {
     console.log('onidentify');
     console.log(identify);
-    // this.collectEvent(identify.json());
+    this.collectEvent(this.buildEvent("identify",
+        this.buildTarget(identify.userId(), "analyticsUser", identify.traits()),
+        this.buildSource(this.options.scope, 'site', identify.context())));
 };
 
 /**
@@ -12701,6 +12703,13 @@ Unomi.prototype.ontrack = function(track) {
         var formEvent = this.buildFormEvent(form.name);
         formEvent.properties = this.extractFormData(form);
         this.collectEvent(formEvent);
+    } else {
+        console.log("Generic event track", track);
+        this.collectEvent(this.buildEvent(track.event(),
+            this.buildTargetPage(),
+            this.buildSource(this.options.scope, 'site', track.context()),
+            track.properties()
+        ));
     }
 };
 
@@ -12787,9 +12796,10 @@ Unomi.prototype.onpersonalize = function (msg) {
  * @param {string} eventType The name of your event
  * @param {object} [target] The target object for your event can be build with this.buildTarget(targetId, targetType, targetProperties)
  * @param {object} [source] The source object for your event can be build with this.buildSource(sourceId, sourceType, sourceProperties)
+ * @param {object} [properties] a map of properties for the event
  * @returns {{eventType: *, scope}}
  */
-Unomi.prototype.buildEvent = function (eventType, target, source) {
+Unomi.prototype.buildEvent = function (eventType, target, source, properties) {
     var event = {
         eventType: eventType,
         scope: window.digitalData.scope
@@ -12803,6 +12813,10 @@ Unomi.prototype.buildEvent = function (eventType, target, source) {
         event.source = source;
     }
 
+    if (properties) {
+        event.properties = properties;
+    }
+
     return event;
 };
 
diff --git a/extensions/web-tracker/javascript/dist/unomi-tracker.min.js b/extensions/web-tracker/javascript/dist/unomi-tracker.min.js
index 4ef5405..eddf39a 100644
--- a/extensions/web-tracker/javascript/dist/unomi-tracker.min.js
+++ b/extensions/web-tracker/javascript/dist/unomi-tracker.min.js
@@ -25,4 +25,4 @@
  * @license MIT
  */
 var r,o=Object.prototype,i=o.hasOwnProperty,a=o.toString;"function"==typeof Symbol&&(r=Symbol.prototype.valueOf);var s=function(t){return t!==t},c={boolean:1,number:1,string:1,undefined:1},u=/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/,l=/^[A-Fa-f0-9]+$/,p={};p.a=p.type=function(t,e){return typeof t===e},p.defined=function(t){return void 0!==t},p.empty=function(t){var e,n=a.call(t);if("[object Array]"===n||"[object Arguments]"===n||"[object String]"= [...]
-r.prototype.checkoutId=function(){return this.proxy("properties.checkout_id")||this.proxy("properties.checkoutId")},r.prototype.paymentId=function(){return this.proxy("properties.payment_id")||this.proxy("properties.paymentId")},r.prototype.couponId=function(){return this.proxy("properties.coupon_id")||this.proxy("properties.couponId")},r.prototype.wishlistId=function(){return this.proxy("properties.wishlist_id")||this.proxy("properties.wishlistId")},r.prototype.reviewId=function(){retur [...]
\ No newline at end of file
+r.prototype.checkoutId=function(){return this.proxy("properties.checkout_id")||this.proxy("properties.checkoutId")},r.prototype.paymentId=function(){return this.proxy("properties.payment_id")||this.proxy("properties.paymentId")},r.prototype.couponId=function(){return this.proxy("properties.coupon_id")||this.proxy("properties.couponId")},r.prototype.wishlistId=function(){return this.proxy("properties.wishlist_id")||this.proxy("properties.wishlistId")},r.prototype.reviewId=function(){retur [...]
\ No newline at end of file
diff --git a/extensions/web-tracker/javascript/src/analytics.js-integration-apache-unomi.js b/extensions/web-tracker/javascript/src/analytics.js-integration-apache-unomi.js
index 6ed3ddc..338922a 100644
--- a/extensions/web-tracker/javascript/src/analytics.js-integration-apache-unomi.js
+++ b/extensions/web-tracker/javascript/src/analytics.js-integration-apache-unomi.js
@@ -170,9 +170,9 @@ Unomi.prototype.processReferrer = function() {
  * @param {Identify} identify
  */
 Unomi.prototype.onidentify = function(identify) {
-    console.log('onidentify');
-    console.log(identify);
-    // this.collectEvent(identify.json());
+    this.collectEvent(this.buildEvent("identify",
+        this.buildTarget(identify.userId(), "analyticsUser", identify.traits()),
+        this.buildSource(this.options.scope, 'site', identify.context())));
 };
 
 /**
@@ -189,6 +189,12 @@ Unomi.prototype.ontrack = function(track) {
         var formEvent = this.buildFormEvent(form.name);
         formEvent.properties = this.extractFormData(form);
         this.collectEvent(formEvent);
+    } else {
+        this.collectEvent(this.buildEvent(track.event(),
+            this.buildTargetPage(),
+            this.buildSource(this.options.scope, 'site', track.context()),
+            track.properties()
+        ));
     }
 };
 
@@ -275,9 +281,10 @@ Unomi.prototype.onpersonalize = function (msg) {
  * @param {string} eventType The name of your event
  * @param {object} [target] The target object for your event can be build with this.buildTarget(targetId, targetType, targetProperties)
  * @param {object} [source] The source object for your event can be build with this.buildSource(sourceId, sourceType, sourceProperties)
+ * @param {object} [properties] a map of properties for the event
  * @returns {{eventType: *, scope}}
  */
-Unomi.prototype.buildEvent = function (eventType, target, source) {
+Unomi.prototype.buildEvent = function (eventType, target, source, properties) {
     var event = {
         eventType: eventType,
         scope: window.digitalData.scope
@@ -291,6 +298,10 @@ Unomi.prototype.buildEvent = function (eventType, target, source) {
         event.source = source;
     }
 
+    if (properties) {
+        event.properties = properties;
+    }
+
     return event;
 };
 
diff --git a/extensions/web-tracker/wab/src/main/webapp/index.html b/extensions/web-tracker/wab/src/main/webapp/index.html
index c92bb62..5c8e95b 100644
--- a/extensions/web-tracker/wab/src/main/webapp/index.html
+++ b/extensions/web-tracker/wab/src/main/webapp/index.html
@@ -43,7 +43,7 @@
     <script type="text/javascript">
         var path = location.pathname + location.hash;
         var unomiOption = {
-            scope: 'realEstateManager',
+            scope: 'myScope',
             url: '', // we use an empty URL to make it relative to this page.
             initialPageProperties: {
                 path: path,
@@ -128,6 +128,18 @@
             document.addEventListener('DOMContentLoaded', unomiTracker.load);
 
             unomiTracker.page(unomiOption.initialPageProperties);
+
+            // these examples come from the Analytics.JS documentation:
+            unomiTracker.identify({
+                nickname: 'Amazing Grace',
+                favoriteCompiler: 'A-0',
+                industry: 'Computer Science'
+            });
+
+            unomiTracker.track('articleCompleted', {
+                title: 'How to Create a Tracking Plan',
+                course: 'Intro to Analytics'
+            });
         })();
     </script>
 </head>