http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e13e8a10/extensions/web-tracker/wab/package.json ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/package.json b/extensions/web-tracker/wab/package.json deleted file mode 100644 index e955dc0..0000000 --- a/extensions/web-tracker/wab/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "analytics.js-integration-apache-unomi", - "version": "1.0.0", - "description": "The Apache Unomi analytics.js integration.", - "main": "src/main/javascript/index.js", - "license": "APACHE", - "scripts": { - "build": "yarn clean && browserify src/main/javascript/index.js -s unomiTracker > src/main/webapp/javascript/unomi-tracker.js", - "minify": "yarn clean && yarn build && uglifyjs src/main/webapp/javascript/unomi-tracker.js -o src/main/webapp/javascript/unomi-tracker.min.js", - "clean": "rm -rf *.log src/main/webapp/javascript/unomi-tracker.js src/main/webapp/javascript/unomi-tracker.min.js", - "clean:all": "yarn clean && rm -rf node_modules" - }, - "dependencies": { - "@segment/analytics.js-core": "^3.7.2", - "@segment/analytics.js-integration": "^2.1.1" - }, - "devDependencies": { - "@segment/eslint-config": "^3.1.1", - "browserify": "^13.0.1", - "eslint": "^2.9.0", - "eslint-plugin-mocha": "^2.2.0", - "eslint-plugin-require-path-exists": "^1.1.5", - "uglify-js": "^2.6.4" - } -}
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e13e8a10/extensions/web-tracker/wab/src/main/javascript/analytics.js-integration-apache-unomi.js ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/src/main/javascript/analytics.js-integration-apache-unomi.js b/extensions/web-tracker/wab/src/main/javascript/analytics.js-integration-apache-unomi.js deleted file mode 100644 index dbd3ea5..0000000 --- a/extensions/web-tracker/wab/src/main/javascript/analytics.js-integration-apache-unomi.js +++ /dev/null @@ -1,483 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -'use strict'; - -var integration = require('@segment/analytics.js-integration'); - -var Unomi = module.exports = integration('Apache Unomi') - .assumesPageview() - .readyOnLoad() - .global('cxs') - .option('scope', 'systemscope') - .option('url', 'http://localhost:8181') - .option('timeoutInMilliseconds', 1500) - .option('sessionCookieName', 'unomiSessionId') - .option('sessionId'); - -/** - * Initialize. - * - * @api public - */ -Unomi.prototype.initialize = function(page) { - var self = this; - this.analytics.on('invoke', function(msg) { - var action = msg.action(); - var listener = 'on' + msg.action(); - self.debug('%s %o', action, msg); - if (self[listener]) self[listener](msg); - }); - - // Standard to check if cookies are enabled in this browser - if (!navigator.cookieEnabled) { - this.executeFallback(); - return; - } - - // digitalData come from a standard so we can keep the logic around it which can allow complex website to load more complex data - if (!window.digitalData) { - window.digitalData = { - scope: this.options.scope - }; - } - - if (page) { - var props = page.json().properties; - var unomiPage = window.digitalData.page; - if (!unomiPage) { - unomiPage = window.digitalData.page = { pageInfo:{} } - } - this.fillPageData(unomiPage, props); - if (!window.digitalData.events) { - window.digitalData.events = [] - } - window.digitalData.events.push(this.buildEvent('view', this.buildPage(unomiPage), this.buildSource(this.options.scope, 'site'))) - } - - if (!this.options.sessionId) { - var cookie = require('component-cookie'); - - this.sessionId = cookie(this.options.sessionCookieName); - // so we should not need to implement our own - if (!this.sessionId || this.sessionId === '') { - this.sessionId = this.generateGuid(); - cookie(this.options.sessionCookieName, this.sessionId); - } - } else { - this.sessionId = this.options.sessionId; - } - - - this.loadContext(); -}; - -/** - * Loaded. - * - * @api private - * @return {boolean} - */ -Unomi.prototype.loaded = function() { - return !!window.cxs; -}; - -/** - * Page. - * - * @api public - * @param {Page} page - */ -Unomi.prototype.onpage = function(page) { - console.log('onpage'); - console.log(page); - - var unomiPage = { pageInfo:{} }; - this.fillPageData(unomiPage, page.json().properties); - console.log(unomiPage); - - this.collectEvent(this.buildEvent('view', this.buildPage(unomiPage), this.buildSource(this.options.scope, 'site'))); -}; - -Unomi.prototype.fillPageData = function(unomiPage, props) { - unomiPage.attributes = []; - unomiPage.consentTypes = []; - unomiPage.pageInfo.pageName = props.title; - unomiPage.pageInfo.pageID = props.path; - unomiPage.pageInfo.pagePath = props.path; - unomiPage.pageInfo.destinationURL = props.url; - unomiPage.pageInfo.referringURL = props.referrer; -} - - -/** - * Identify. - * - * @api public - * @param {Identify} identify - */ -Unomi.prototype.onidentify = function(identify) { - console.log('onidentify'); - console.log(identify); - // this.collectEvent(identify.json()); -}; - -/** - * ontrack. - * - * @api private - * @param {Track} track - */ -// TODO: figure out why we need this. -Unomi.prototype.ontrack = function(track) { - console.log('ontrack'); - console.log(track); - // var json = track.json(); - - // delete json.traits; - // this.collectEvent(json); -}; - -/** - * This function is used to load the current context in the page - * - * @param {boolean} [skipEvents=false] Should we send the events - * @param {boolean} [invalidate=false] Should we invalidate the current context - */ -Unomi.prototype.loadContext = function (skipEvents, invalidate) { - var jsonData = { - requiredProfileProperties: ['j:nodename'], - source: this.buildPage(window.digitalData.page) - }; - if (!skipEvents) { - jsonData.events = window.digitalData.events - } - if (window.digitalData.personalizationCallback) { - jsonData.personalizations = window.digitalData.personalizationCallback.map(function (x) { - return x.personalization - }) - } - - jsonData.sessionId = this.sessionId; - - var contextUrl = this.options.url + '/context.json'; - if (invalidate) { - contextUrl += '?invalidateSession=true&invalidateProfile=true'; - } - - var self = this; - - var onSuccess = function (xhr) { - - window.cxs = JSON.parse(xhr.responseText); - - self.ready(); - - if (window.digitalData.loadCallbacks && window.digitalData.loadCallbacks.length > 0) { - console.info('[UNOMI] Found context server load callbacks, calling now...'); - if (window.digitalData.loadCallbacks) { - for (var i = 0; i < window.digitalData.loadCallbacks.length; i++) { - window.digitalData.loadCallbacks[i](digitalData); - } - } - if (window.digitalData.personalizationCallback) { - for (var i = 0; i < window.digitalData.personalizationCallback.length; i++) { - window.digitalData.personalizationCallback[i].callback(cxs.personalizations[window.digitalData.personalizationCallback[i].personalization.id]); - } - } - } - }; - - this.ajax({ - url: contextUrl, - type: 'POST', - async: true, - contentType: 'text/plain;charset=UTF-8', // Use text/plain to avoid CORS preflight - jsonData: jsonData, - dataType: 'application/json', - invalidate: invalidate, - success: onSuccess, - error: this.executeFallback - }); - - console.info('[UNOMI] context loading...'); -}; - -/** - * This function return the basic structure for an event, it must be adapted to your need - * - * @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) - * @returns {{eventType: *, scope}} - */ -Unomi.prototype.buildEvent = function (eventType, target, source) { - var event = { - eventType: eventType, - scope: window.digitalData.scope - }; - - if (target) { - event.target = target; - } - - if (source) { - event.source = source; - } - - return event; -}; - -/** - * This function return an event of type form - * - * @param {string} formName The HTML name of id of the form to use in the target of the event - * @returns {*|{eventType: *, scope, source: {scope, itemId: string, itemType: string, properties: {}}, target: {scope, itemId: string, itemType: string, properties: {}}}} - */ -Unomi.prototype.buildFormEvent = function (formName) { - return this.buildEvent('form', this.buildTarget(formName, 'form'), this.buildSourcePage()); -}; - -/** - * This function return the source object for a source of type page - * - * @returns {*|{scope, itemId: *, itemType: *}} - */ -Unomi.prototype.buildTargetPage = function () { - return this.buildTarget(window.digitalData.page.pageInfo.pageID, 'page', window.digitalData.page); -}; - -/** - * This function return the source object for a source of type page - * - * @returns {*|{scope, itemId: *, itemType: *}} - */ -Unomi.prototype.buildPage = function (page) { - return this.buildSource(page.pageInfo.pageID, 'page', page); -}; - -/** - * This function return the basic structure for the target of your event - * - * @param {string} targetId The ID of the target - * @param {string} targetType The type of the target - * @param {object} [targetProperties] The optional properties of the target - * @returns {{scope, itemId: *, itemType: *}} - */ -Unomi.prototype.buildTarget = function (targetId, targetType, targetProperties) { - return this.buildObject(targetId, targetType, targetProperties); -}; - -/** - * This function return the basic structure for the source of your event - * - * @param {string} sourceId The ID of the source - * @param {string} sourceType The type of the source - * @param {object} [sourceProperties] The optional properties of the source - * @returns {{scope, itemId: *, itemType: *}} - */ -Unomi.prototype.buildSource = function (sourceId, sourceType, sourceProperties) { - return this.buildObject(sourceId, sourceType, sourceProperties); -}; - - -/** - * This function will send an event to Apache Unomi - * @param {object} event The event object to send, you can build it using this.buildEvent(eventType, target, source) - * @param {function} successCallback will be executed in case of success - * @param {function} errorCallback will be executed in case of error - */ -Unomi.prototype.collectEvent = function (event, successCallback, errorCallback) { - this.collectEvents({events: [event]}, successCallback, errorCallback); -}; - -/** - * This function will send the events to Apache Unomi - * - * @param {object} events Javascript object { events: [event1, event2] } - * @param {function} successCallback will be executed in case of success - * @param {function} errorCallback will be executed in case of error - */ -Unomi.prototype.collectEvents = function (events, successCallback, errorCallback) { - events.sessionId = this.sessionId; - - var data = JSON.stringify(events); - this.ajax({ - url: this.options.url + '/eventcollector', - type: 'POST', - async: true, - contentType: 'text/plain;charset=UTF-8', // Use text/plain to avoid CORS preflight - data: data, - dataType: 'application/json', - success: successCallback, - error: errorCallback - }); -}; - -/*******************************/ -/* Private Function under this */ -/*******************************/ - -Unomi.prototype.registerEvent = function (event) { - if (window.digitalData) { - if (window.cxs) { - console.error('[UNOMI] already loaded, too late...'); - } else { - window.digitalData.events = window.digitalData.events || []; - window.digitalData.events.push(event); - } - } else { - window.digitalData = {}; - window.digitalData.events = window.digitalData.events || []; - window.digitalData.events.push(event); - } -}; - -Unomi.prototype.registerCallback = function (onLoadCallback) { - if (window.digitalData) { - if (window.cxs) { - console.info('[UNOMI] digitalData object loaded, calling on load callback immediately and registering update callback...'); - if (onLoadCallback) { - onLoadCallback(window.digitalData); - } - } else { - console.info('[UNOMI] digitalData object present but not loaded, registering load callback...'); - if (onLoadCallback) { - window.digitalData.loadCallbacks = window.digitalData.loadCallbacks || []; - window.digitalData.loadCallbacks.push(onLoadCallback); - } - } - } else { - console.info('[UNOMI] No digital data object found, creating and registering update callback...'); - window.digitalData = {}; - if (onLoadCallback) { - window.digitalData.loadCallbacks = []; - window.digitalData.loadCallbacks.push(onLoadCallback); - } - } -}; - -/** - * This is an utility function to generate a new UUID - * - * @returns {string} - */ -Unomi.prototype.generateGuid = function () { - function s4() { - return Math.floor((1 + Math.random()) * 0x10000) - .toString(16) - .substring(1); - } - - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + - s4() + '-' + s4() + s4() + s4(); -}; - -Unomi.prototype.buildObject = function (itemId, itemType, properties) { - var object = { - scope: window.digitalData.scope, - itemId: itemId, - itemType: itemType - }; - - if (properties) { - object.properties = properties; - } - - return object; -}; - -/** - * This is an utility function to execute AJAX call - * - * @param {object} ajaxOptions - */ -Unomi.prototype.ajax = function (ajaxOptions) { - var xhr = new XMLHttpRequest(); - if ('withCredentials' in xhr) { - xhr.open(ajaxOptions.type, ajaxOptions.url, ajaxOptions.async); - xhr.withCredentials = true; - } else if (typeof XDomainRequest != 'undefined') { - xhr = new XDomainRequest(); - xhr.open(ajaxOptions.type, ajaxOptions.url); - } - - if (ajaxOptions.contentType) { - xhr.setRequestHeader('Content-Type', ajaxOptions.contentType); - } - if (ajaxOptions.dataType) { - xhr.setRequestHeader('Accept', ajaxOptions.dataType); - } - - if (ajaxOptions.responseType) { - xhr.responseType = ajaxOptions.responseType; - } - - var requestExecuted = false; - if (this.options.timeoutInMilliseconds !== -1) { - setTimeout(function () { - if (!requestExecuted) { - console.error('[UNOMI] XML request timeout, url: ' + ajaxOptions.url); - requestExecuted = true; - if (ajaxOptions.error) { - ajaxOptions.error(xhr); - } - } - }, this.options.timeoutInMilliseconds); - } - - xhr.onreadystatechange = function () { - if (!requestExecuted) { - if (xhr.readyState === 4) { - if (xhr.status === 200 || xhr.status === 204 || xhr.status === 304) { - if (xhr.responseText != null) { - requestExecuted = true; - if (ajaxOptions.success) { - ajaxOptions.success(xhr); - } - } - } else { - requestExecuted = true; - if (ajaxOptions.error) { - ajaxOptions.error(xhr); - } - console.error('[UNOMI] XML request error: ' + xhr.statusText + ' (' + xhr.status + ')'); - } - } - } - }; - - if (ajaxOptions.jsonData) { - xhr.send(JSON.stringify(ajaxOptions.jsonData)); - } else if (ajaxOptions.data) { - xhr.send(ajaxOptions.data); - } else { - xhr.send(); - } -}; - -Unomi.prototype.executeFallback = function () { - console.warn('[UNOMI] execute fallback'); - window.cxs = {}; - for (var index in window.digitalData.loadCallbacks) { - window.digitalData.loadCallbacks[index](); - } - if (window.digitalData.personalizationCallback) { - for (var i = 0; i < window.digitalData.personalizationCallback.length; i++) { - window.digitalData.personalizationCallback[i].callback([window.digitalData.personalizationCallback[i].personalization.strategyOptions.fallback]); - } - } -}; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e13e8a10/extensions/web-tracker/wab/src/main/javascript/index.js ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/src/main/javascript/index.js b/extensions/web-tracker/wab/src/main/javascript/index.js deleted file mode 100644 index f2b4e3a..0000000 --- a/extensions/web-tracker/wab/src/main/javascript/index.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * Analytics.js - * - * (C) 2017 Segment Inc. - */ - -var analytics = require('@segment/analytics.js-core'); -var Integrations = require('./integrations'); - -/** - * Expose the `analytics` singleton. - */ - -module.exports = exports = analytics; - -/** - * Expose require. - */ - -analytics.require = require; - -/** - * Expose `VERSION`. - */ - -exports.VERSION = require('../../../package.json').version; - -/** - * Add integrations. - */ - -for (var integration in Integrations) { - analytics.use(Integrations[integration]); -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e13e8a10/extensions/web-tracker/wab/src/main/javascript/integrations.js ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/src/main/javascript/integrations.js b/extensions/web-tracker/wab/src/main/javascript/integrations.js deleted file mode 100644 index 7d2ebcd..0000000 --- a/extensions/web-tracker/wab/src/main/javascript/integrations.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* eslint quote-props: 0 */ -'use strict'; - -module.exports = { - 'apache-unomi': require('./analytics.js-integration-apache-unomi') -}; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/e13e8a10/extensions/web-tracker/wab/src/main/webapp/index.html ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/src/main/webapp/index.html b/extensions/web-tracker/wab/src/main/webapp/index.html index 52bb8ee..5d2e2c9 100644 --- a/extensions/web-tracker/wab/src/main/webapp/index.html +++ b/extensions/web-tracker/wab/src/main/webapp/index.html @@ -14,94 +14,30 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Test</title> - <meta name="Date-Revision-yyyymmdd" content="20180919" /> - <meta http-equiv="Content-Language" content="en" /> + <meta name="Date-Revision-yyyymmdd" content="20180919"/> + <meta http-equiv="Content-Language" content="en"/> <script type="text/javascript"> - window.unomiTracker || (window.unomiTracker = {}); - window.unomiTracker_queue || (window.unomiTracker_queue = []); - (function() { - var methods = ['trackSubmit', 'trackClick', 'trackLink', 'trackForm', 'initialize', 'pageview', 'identify', 'reset', 'group', 'track', 'ready', 'alias', 'debug', 'page', 'once', 'off', 'on']; - - var factory = function(method) { - return function () { - var args = Array.prototype.slice.call(arguments); - args.unshift(method); - window.unomiTracker_queue.push(args); - return window.unomiTracker; - }; - }; - - // For each of our methods, generate a queueing stub. - for (var i = 0; i < methods.length; i++) { - var method = methods[i]; - window.unomiTracker[method] = factory(method); - } - })(); - - // Define a method to load Analytics.js from our CDN, - // and that will be sure to only ever load it once. - unomiTracker.load = function(callback, option) { - // Create an async script element based on your key. - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.async = true; - // TODO we might want to add a check on the url to see if it ends with / or not - script.src = option.url + '/tracker/javascript/unomi-tracker.js'; - - if (script.addEventListener) { - script.addEventListener('load', function(e) { - if (typeof callback === 'function') { - callback(e); - } - }, false); - } else { - script.onreadystatechange = function () { - if (this.readyState == 'complete' || this.readyState == 'loaded') { - callback(window.event); - } - }; - } - - // Insert our script next to the first script element. - var first = document.getElementsByTagName('script')[0]; - first.parentNode.insertBefore(script, first); - }; - - var option = { + var unomiOption = { scope: 'realEstateManager', url: 'http://localhost:8181' }; + // Copy snippet from snippet.min.js : + window.unomiTracker||(window.unomiTracker={}),function(){for(var e=[],r=["trackSubmit","trackClick","trackLink","trackForm","initialize","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"],n=0;n<r.length;n++){var t=r[n];window.unomiTracker[t]=function(r){return function(){var n=Array.prototype.slice.call(arguments);return n.unshift(r),e.push(n),window.unomiTracker}}(t)}unomiTracker.load=function(e,r){var n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src=r.url+"/tracker/javascript/unomi-tracker.js",n.addEventListener?n.addEventListener("load",function(r){"function"==typeof e&&e(r)},!1):n.onreadystatechange=function(){"complete"!=this.readyState&&"loaded"!=this.readyState||e(window.event)};var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(n,t)},unomiTracker.load(function(r){for(unomiTracker.initialize({"Apache Unomi":r});e.length>0;){var n=e.shift(),t=n.shift();unomiTracker[t]&&unom iTracker[t].apply(unomiTracker,n)}},unomiOption),unomiTracker.page()}(); - unomiTracker.load(function () { - unomiTracker.initialize({ - 'Apache Unomi': option - }); - - // Loop through the interim analytics queue and reapply the calls to their - // proper analytics.js method. - while (window.unomiTracker_queue.length > 0) { - var item = window.unomiTracker_queue.shift(); - var method = item.shift(); - if (unomiTracker[method]) { - unomiTracker[method].apply(unomiTracker, item); - } - } - }, option); - - unomiTracker.page(); </script> </head> <body class="composite"> - Test unomi +Test unomi </body> </html>