UNOMI-187 created unomi plugin for analytics.js, cleanup files, renamed web-tracker-core to web-tracker-wab, renamed analytics.js file to unomi-tracker.js and global variable analytics to unomiTracker so we can use the tracket event there is a version of analytics js on the site
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/b32fce9e Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/b32fce9e Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/b32fce9e Branch: refs/heads/master Commit: b32fce9e9a5d2246f927fa427343a54f85c64f0f Parents: 191fbba Author: dgaillard <[email protected]> Authored: Wed Sep 19 11:30:00 2018 +0200 Committer: dgaillard <[email protected]> Committed: Wed Sep 19 11:30:18 2018 +0200 ---------------------------------------------------------------------- extensions/web-tracker/README.md | 81 + extensions/web-tracker/core/pom.xml | 73 - .../resources/OSGI-INF/blueprint/blueprint.xml | 22 - .../src/main/resources/messages_en.properties | 16 - extensions/web-tracker/karaf-kar/pom.xml | 2 +- .../karaf-kar/src/main/feature/feature.xml | 2 +- extensions/web-tracker/pom.xml | 2 +- extensions/web-tracker/wab/.gitignore | 3 + extensions/web-tracker/wab/package.json | 25 + extensions/web-tracker/wab/pom.xml | 75 + .../analytics.js-integration-apache-unomi.js | 447 + .../wab/src/main/javascript/index.js | 50 + .../wab/src/main/javascript/integrations.js | 22 + .../src/main/webapp/javascript/unomi-tracker.js | 13069 +++++++++++++++++ extensions/web-tracker/wab/yarn.lock | 2072 +++ kar/pom.xml | 2 +- pom.xml | 4 + .../resources/OSGI-INF/blueprint/blueprint.xml | 2 +- 18 files changed, 15853 insertions(+), 116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/README.md ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/README.md b/extensions/web-tracker/README.md index ae99847..2cc182a 100644 --- a/extensions/web-tracker/README.md +++ b/extensions/web-tracker/README.md @@ -22,3 +22,84 @@ Apache Unomi Web Tracker This extension is providing the web tracker to start collecting visitors data on your website ## Getting started + +```html +<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) { + console.log('load'); + // 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/dist/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 = { + scope: 'realEstateManager', + url: 'http://localhost:8181' + }; + + 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.ready(function () { + unomiTracker.page(); + }); +</script> +``` \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/core/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/core/pom.xml b/extensions/web-tracker/core/pom.xml deleted file mode 100755 index d29df77..0000000 --- a/extensions/web-tracker/core/pom.xml +++ /dev/null @@ -1,73 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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. - --> - -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.unomi</groupId> - <artifactId>unomi-web-tracker</artifactId> - <version>1.4.0-incubating-SNAPSHOT</version> - </parent> - - <artifactId>unomi-web-tracker-core</artifactId> - <name>Apache Unomi :: Extension :: Web Tracker :: Core</name> - <description>This is a simple Apache Unomi plugin.</description> - <packaging>bundle</packaging> - - <dependencies> - <dependency> - <groupId>org.apache.unomi</groupId> - <artifactId>unomi-api</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient-osgi</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpcore-osgi</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <scope>provided</scope> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> - <Import-Package> - * - </Import-Package> - </instructions> - </configuration> - </plugin> - </plugins> - </build> -</project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/web-tracker/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml deleted file mode 100755 index 325170a..0000000 --- a/extensions/web-tracker/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ /dev/null @@ -1,22 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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. - --> -<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" - xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" - xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> -</blueprint> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/core/src/main/resources/messages_en.properties ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/core/src/main/resources/messages_en.properties b/extensions/web-tracker/core/src/main/resources/messages_en.properties deleted file mode 100755 index 86094c8..0000000 --- a/extensions/web-tracker/core/src/main/resources/messages_en.properties +++ /dev/null @@ -1,16 +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. -# http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/karaf-kar/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/karaf-kar/pom.xml b/extensions/web-tracker/karaf-kar/pom.xml index 8b3e21b..5826931 100644 --- a/extensions/web-tracker/karaf-kar/pom.xml +++ b/extensions/web-tracker/karaf-kar/pom.xml @@ -47,7 +47,7 @@ <dependency> <groupId>org.apache.unomi</groupId> - <artifactId>unomi-web-tracker-core</artifactId> + <artifactId>unomi-web-tracker-wab</artifactId> <version>${project.version}</version> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/karaf-kar/src/main/feature/feature.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/karaf-kar/src/main/feature/feature.xml b/extensions/web-tracker/karaf-kar/src/main/feature/feature.xml index 5835a41..cc72fd0 100644 --- a/extensions/web-tracker/karaf-kar/src/main/feature/feature.xml +++ b/extensions/web-tracker/karaf-kar/src/main/feature/feature.xml @@ -23,6 +23,6 @@ <!--.version}/cfg/weatherupdatecfg</configfile>--> <bundle start-level="85">mvn:org.apache.httpcomponents/httpcore-osgi/${httpcore-osgi.version}</bundle> <bundle start-level="85">mvn:org.apache.httpcomponents/httpclient-osgi/${httpclient-osgi.version}</bundle> - <bundle start-level="85">mvn:org.apache.unomi/unomi-web-tracker-core/${project.version}</bundle> + <bundle start-level="85">mvn:org.apache.unomi/unomi-web-tracker-wab/${project.version}</bundle> </feature> </features> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/pom.xml b/extensions/web-tracker/pom.xml index 2a77baa..efa80d8 100644 --- a/extensions/web-tracker/pom.xml +++ b/extensions/web-tracker/pom.xml @@ -30,7 +30,7 @@ <packaging>pom</packaging> <modules> - <module>core</module> + <module>wab</module> <module>karaf-kar</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/wab/.gitignore ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/.gitignore b/extensions/web-tracker/wab/.gitignore new file mode 100644 index 0000000..6ade2e9 --- /dev/null +++ b/extensions/web-tracker/wab/.gitignore @@ -0,0 +1,3 @@ +node_modules +yarn-error.log +src/main/webapp/dist \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/extensions/web-tracker/wab/package.json ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/package.json b/extensions/web-tracker/wab/package.json new file mode 100644 index 0000000..e955dc0 --- /dev/null +++ b/extensions/web-tracker/wab/package.json @@ -0,0 +1,25 @@ +{ + "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/b32fce9e/extensions/web-tracker/wab/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/web-tracker/wab/pom.xml b/extensions/web-tracker/wab/pom.xml new file mode 100755 index 0000000..f441be1 --- /dev/null +++ b/extensions/web-tracker/wab/pom.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.unomi</groupId> + <artifactId>unomi-web-tracker</artifactId> + <version>1.4.0-incubating-SNAPSHOT</version> + </parent> + + <artifactId>unomi-web-tracker-wab</artifactId> + <name>Apache Unomi :: Extension :: Web Tracker :: Wab</name> + <description>This is a simple Apache Unomi plugin.</description> + <packaging>bundle</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.unomi</groupId> + <artifactId>unomi-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient-osgi</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpcore-osgi</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <_wab>src/main/webapp</_wab> + <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> + <Import-Package> + * + </Import-Package> + <Web-ContextPath>/tracker</Web-ContextPath> + </instructions> + </configuration> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b32fce9e/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 new file mode 100644 index 0000000..e0e3620 --- /dev/null +++ b/extensions/web-tracker/wab/src/main/javascript/analytics.js-integration-apache-unomi.js @@ -0,0 +1,447 @@ +/* + * 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') + .readyOnLoad() + .global('cxs') + .option('scope', 'systemscope') + .option('url', 'http://localhost:8181') + .option('timeoutInMilliseconds', 1500) + .option('sessionId'); + +/** + * Initialize. + * + * @api public + */ +Unomi.prototype.initialize = function() { + 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) { + _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 = {}; + } + + if (!this.options.sessionId) { + this.options.sessionId = '';// get sessionId from cookie, there are some method to deal with cookie in the core/integration of + // analytics js + // so we should not need to implement our own + if (!this.options.sessionId || this.options.sessionId === '') { + this.options.sessionId = _generateGuid(); + } + } + + 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); + console.log(page.json()); + console.log(this.options); + console.log(window.cxs); + + var properties = page.json().properties; + properties.pageInfo = {}; + properties.pageInfo.tags = ['toto', 'tata', 'titi']; + + this.collectEvent(this.buildEvent('view', this.buildTarget(page.json().properties.path, 'page', properties), this.buildSource(this.options.scope, 'site'))); +}; + +/** + * 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.buildSourcePage() + }; + 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.options.sessionId; + + var contextUrl = this.options.url + '/context.json'; + if (invalidate) { + contextUrl += '?invalidateSession=true&invalidateProfile=true'; + } + _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: _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.buildSourcePage = function () { + return this.buildSource(window.digitalData.page.pageInfo.pageID, 'page', window.digitalData.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 _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 _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.options.sessionId; + + var data = JSON.stringify(events); + _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} + */ +var _generateGuid = function () { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + + s4() + '-' + s4() + s4() + s4(); +}; + +var _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 + */ +var _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(); + } +}; + +var _onSuccess = function (xhr) { + window.cxs = JSON.parse(xhr.responseText); + + 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]); + } + } + } +}; + +var _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/b32fce9e/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 new file mode 100644 index 0000000..f2b4e3a --- /dev/null +++ b/extensions/web-tracker/wab/src/main/javascript/index.js @@ -0,0 +1,50 @@ +/* + * 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/b32fce9e/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 new file mode 100644 index 0000000..7d2ebcd --- /dev/null +++ b/extensions/web-tracker/wab/src/main/javascript/integrations.js @@ -0,0 +1,22 @@ +/* + * 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') +};
