This is an automated email from the ASF dual-hosted git repository.
sergehuber pushed a commit to branch 1_x
in repository https://gitbox.apache.org/repos/asf/unomi-tracker.git
The following commit(s) were added to refs/heads/1_x by this push:
new 9dc0f07 UNOMI-1037, UNOMI-1038, UNOMI-1040 to UNOMI-1043, UNOMI-1046,
UNOMI-1047: Backport the click and form tracking fixes to 1.x (#41)
9dc0f07 is described below
commit 9dc0f07bc14eab648e3e2d373fac052ef402dfc8
Author: Serge Huber <[email protected]>
AuthorDate: Wed Nov 4 20:53:17 2026 +0100
UNOMI-1037, UNOMI-1038, UNOMI-1040 to UNOMI-1043, UNOMI-1046, UNOMI-1047:
Backport the click and form tracking fixes to 1.x (#41)
The fixes of #39 on main, for tracker 1.6.0:
- UNOMI-1037: every click on a link or button is tracked, not only the
first one, also a second click made before the first event is sent.
- UNOMI-1040: links and forms work at once when Unomi cannot be reached
or tracking is off, and wait 1.5s at most when there is no timeout.
- UNOMI-1041: Ctrl or Cmd+click opens a new tab, and is still tracked.
- UNOMI-1042: a tracked form is submitted with the button that was used.
- UNOMI-1043: without a usable timeoutInMilliseconds, the default of
1500 ms is used, instead of every request failing at once.
- UNOMI-1046: the site's own click handlers run once per click.
- UNOMI-1047: a click inside a tracked link or button is tracked on it.
- UNOMI-1038: sourceLocalIdentifierMap finds a mapped part anywhere in
the id, also at its start.
The browser tests of #39 come too, against Unomi 2.3, 2.7 and 3.0, in
Chromium, Firefox and WebKit, and the Safari checks (yarn test:safari).
Left out, as 1.x does not have them: Unomi 4, apiKey and sendCredentials
tests, the CI workflow, and the README sections on 2.x. The test setup
also waits until the server answers steadily before the tests, as on
main: a starting Unomi 2.7 dropped its endpoints for a while. dist is
not rebuilt, as in #29.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
package.json | 1 +
src/apache-unomi-tracker.js | 361 +++++++++++++++++++++++++----
test/servers/README.md | 61 +++++
test/servers/page/index.html | 208 ++++++++++++++++-
test/servers/playwright.config.js | 21 +-
test/servers/safari.js | 475 ++++++++++++++++++++++++++++++++++++++
test/servers/serve.js | 15 +-
test/servers/tracker.spec.js | 437 ++++++++++++++++++++++++++++++++++-
test/servers/unomi.js | 87 ++++++-
test/spec.js | 253 +++++++++++++++++++-
10 files changed, 1844 insertions(+), 75 deletions(-)
diff --git a/package.json b/package.json
index 6eceb1f..6102a96 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"build": "rollup -c",
"test": "node test/spec",
"test:servers": "node test/servers/run.js",
+ "test:safari": "node test/servers/safari.js",
"pretest": "yarn run build",
"lint": "eslint --ext js,json ."
},
diff --git a/src/apache-unomi-tracker.js b/src/apache-unomi-tracker.js
index f64abbf..173224d 100644
--- a/src/apache-unomi-tracker.js
+++ b/src/apache-unomi-tracker.js
@@ -17,6 +17,147 @@
import { Crawler } from 'es6-crawler-detect';
+/**
+ * Whether a link or form opening in the named browsing context replaces the
current page: no name,
+ * or _self, _parent or _top. Another name opens another tab or window, and
the page stays.
+ * @param {string | null} name the target or formtarget attribute
+ * @return {boolean} true if the current page is replaced
+ */
+function opensInThisPage(name) {
+ var context = (name || '').trim().toLowerCase();
+ return context === '' || context === '_self' || context === '_parent' ||
context === '_top';
+}
+
+/**
+ * Whether a click on the element itself leaves the page: a link with an
address that opens in
+ * this page (not in another tab or window, not a download, a #fragment or
javascript:), or a
+ * button that submits its form into this page
+ * @param {Element} element an element
+ * @return {boolean} true if the click leaves the page
+ */
+function leavesPage(element) {
+ var tag = element.localName;
+ if (tag === 'a' || tag === 'area') {
+ var href = element.getAttribute('href');
+ if (href === null) {
+ return false;
+ }
+ href = href.trim();
+ if (href.charAt(0) === '#' || /^javascript:/i.test(href) ||
element.hasAttribute('download')) {
+ return false;
+ }
+ return opensInThisPage(element.getAttribute('target'));
+ }
+ var control = /** @type {HTMLButtonElement | HTMLInputElement} */(element);
+ var submits = (tag === 'button' && control.type === 'submit') || (tag ===
'input' && (control.type === 'submit' || control.type === 'image'));
+ if (!submits || !control.form) {
+ return false;
+ }
+ return opensInThisPage(control.getAttribute('formtarget') ||
control.form.getAttribute('target'));
+}
+
+/**
+ * The element whose click leaves the page, from the clicked element up: the
clicked element can be
+ * inside a link or a button (an image, an icon, some text)
+ * @param {Element | null} element the clicked element
+ * @return {Element | null} the link or button, or null if the click stays on
the page
+ */
+function elementLeavingPage(element) {
+ for (var node = element; node; node = node.parentElement) {
+ if (leavesPage(node)) {
+ return node;
+ }
+ }
+ return null;
+}
+
+/**
+ * For an action held back while an event is sent: wraps its callbacks (event
sent, not sent, no
+ * answer) so that only the first one called runs
+ * @return {function(function(XMLHttpRequest=): void):
function(XMLHttpRequest=): void} wraps a callback so
+ * that only the first call runs
+ */
+function settleOnce() {
+ var settled = false;
+ return function (callback) {
+ return function (xhr) {
+ if (!settled) {
+ settled = true;
+ callback(xhr);
+ }
+ };
+ };
+}
+
+/**
+ * Submits a form in browsers without requestSubmit: a copy of the submit
event for the site's
+ * handlers, then submit(), with the submitting button's name and value added
as a native submit does
+ * (for an image button, its click position, as 0,0), and the button's
formaction, formmethod,
+ * formenctype and formtarget applied.
+ * @param {HTMLFormElement} form the form
+ * @param {HTMLButtonElement | HTMLInputElement | null} submitter the button
that submitted it, if any
+ * @return {undefined}
+ */
+function submitWithoutRequestSubmit(form, submitter) {
+ /** @type {Event} */
+ var eventCopy;
+ if (typeof SubmitEvent === 'function') {
+ eventCopy = new SubmitEvent('submit', { bubbles: true, cancelable:
true, submitter: submitter });
+ } else {
+ eventCopy = document.createEvent('Event');
+ eventCopy.initEvent('submit', true, true);
+ }
+ form.dispatchEvent(eventCopy);
+ if (eventCopy.defaultPrevented) {
+ return;
+ }
+ /** @type {HTMLInputElement[]} */
+ var fields = [];
+ if (submitter && submitter.name) {
+ var values = submitter.type === 'image'
+ ? [[submitter.name + '.x', '0'], [submitter.name + '.y', '0']]
+ : [[submitter.name, submitter.value]];
+ values.forEach(function (nameAndValue) {
+ var field = document.createElement('input');
+ field.type = 'hidden';
+ field.name = nameAndValue[0];
+ field.value = nameAndValue[1];
+ form.appendChild(field);
+ fields.push(field);
+ });
+ }
+ // The button's own address, method, encoding and target, as a native
submit uses them
+ /** @type {Array<[string, string | null]>} */
+ var formAttributes = [];
+ if (submitter) {
+ [['formaction', 'action'], ['formmethod', 'method'], ['formenctype',
'enctype'], ['formtarget', 'target']].forEach(function (names) {
+ var value = submitter.getAttribute(names[0]);
+ if (value !== null) {
+ formAttributes.push([names[1], form.getAttribute(names[1])]);
+ form.setAttribute(names[1], value);
+ }
+ });
+ }
+ try {
+ // The data is read when submit() is called
+ HTMLFormElement.prototype.submit.call(form);
+ } finally {
+ fields.forEach(function (field) {
+ form.removeChild(field);
+ });
+ formAttributes.forEach(function (nameAndValue) {
+ if (nameAndValue[1] === null) {
+ form.removeAttribute(nameAndValue[0]);
+ } else {
+ form.setAttribute(nameAndValue[0], nameAndValue[1]);
+ }
+ });
+ }
+}
+
+// Request timeout when the configuration gives none
+const DEFAULT_TIMEOUT_IN_MILLISECONDS = 1500;
+
export const newTracker = () => {
const wem = {
/**
@@ -36,7 +177,11 @@ export const newTracker = () => {
const { contextServerUrl, timeoutInMilliseconds,
contextServerCookieName } = wem.digitalData.wemInitConfig;
wem.contextServerCookieName = contextServerCookieName;
wem.contextServerUrl = contextServerUrl;
- wem.timeoutInMilliseconds = timeoutInMilliseconds;
+ // A positive number of milliseconds, or -1 for none, also given
as a string (as read from
+ // an HTML attribute). Anything else (missing, 0, another negative
number, a blank string)
+ // would make the request timer fire at once and every request
fail, so use the default.
+ var timeout = Number(/** @type {unknown}
*/(timeoutInMilliseconds));
+ wem.timeoutInMilliseconds = isFinite(timeout) && (timeout === -1
|| timeout > 0) ? timeout : DEFAULT_TIMEOUT_IN_MILLISECONDS;
wem.formNamesToWatch = [];
wem.eventsPrevented = [];
wem.sessionID = wem.getCookie(wem.trackerSessionIdCookieName);
@@ -397,44 +542,114 @@ export const newTracker = () => {
/**
* This function will build an event of type click and send it to
Apache Unomi
*
+ * The event is on the element the listener is on
(event.currentTarget), or on the clicked
+ * element when that one has no id or name (as when called from a
listener on the document).
+ * A click that leaves the page (on a link, or on a button that
submits its form, also on an
+ * element inside them) is held back until the event is sent, or could
not be, then
+ * dispatched again, so that the page does not close first; without a
request timeout, it is
+ * held back for 1.5s at most. A click on a link that opens another
tab or window, downloads,
+ * or stays on the page (#fragment, javascript:), a click the site
already prevented, and any
+ * other click are not held back. A click with a modifier key (new tab
or window, download)
+ * is not held back either, unless a callback is given. With a
callback, a held back click is
+ * not dispatched again: the site handles it in the callback. When
tracking is off, or Unomi
+ * could not be reached, the click is left alone and nothing is sent.
+ *
* @param {object} event javascript
* @param {function} [successCallback] optional, will be executed if
case of success
* @param {function} [errorCallback] optional, will be executed if
case of error
* @return {undefined}
*/
sendClickEvent: function (event, successCallback = undefined,
errorCallback = undefined) {
- if (event.target.id || event.target.name) {
+ // The element the listener is on, if it has an id or a name: the
visitor may have clicked
+ // an image, an icon or some text inside it
+ var listening = /** @type {HTMLElement & { name: string } | null}
*/(event.currentTarget);
+ var tracked = listening && typeof listening.getAttribute ===
'function' && (listening.id || listening.name) ? listening : event.target;
+ if (tracked.id || tracked.name) {
+ var targetId = tracked.id ? tracked.id : tracked.name;
+
+ // The click the tracker dispatches again once the event is
sent: let it through
+ if (wem.eventsPrevented.indexOf(targetId) !== -1) {
+ return;
+ }
+ // Tracking is off, or Unomi could not be reached: nothing is
sent, so leave the click alone
+ if (wem.fallback) {
+ return;
+ }
+
console.debug('[WEM] Send click event');
- var targetId = event.target.id ? event.target.id :
event.target.name;
+ var clicked = event.target;
var clickEvent = wem.buildEvent('click',
- wem.buildTarget(targetId, event.target.localName),
+ wem.buildTarget(targetId, tracked.localName),
wem.buildSourcePage());
- var eventIndex = wem.eventsPrevented.indexOf(targetId);
- if (eventIndex !== -1) {
- wem.eventsPrevented.splice(eventIndex, 0);
- } else {
- wem.eventsPrevented.push(targetId);
-
+ // A click with a modifier key leaves the page open, and
dispatching it again would lose
+ // the key. With a callback, the site handles the click
itself, so it is still held back.
+ // A click the site already prevented is the site's: it is not
dispatched again.
+ var mouseEvent = /** @type {MouseEvent} */(/** @type {unknown}
*/(event));
+ var withModifier = mouseEvent.metaKey || mouseEvent.ctrlKey ||
mouseEvent.shiftKey || mouseEvent.altKey;
+ var holdBack = !event.defaultPrevented &&
elementLeavingPage(clicked) !== null &&
+ !(withModifier && !successCallback && !errorCallback);
+ if (holdBack) {
event.preventDefault();
+ }
- var target = event.target;
-
- wem.collectEvent(clickEvent, function (xhr) {
- console.debug('[WEM] Click event successfully
collected.');
- if (successCallback) {
- successCallback(xhr);
- } else {
- target.click();
+ // Dispatch the click again, on the clicked element and to the
whole page as for the
+ // visitor's click: single-page application routers ignore the
visitor's click, which
+ // is prevented, and handle this one. click() runs at once, so
the tracked element is
+ // only marked while it does.
+ var dispatchAgain = function () {
+ wem.eventsPrevented.push(targetId);
+ try {
+ clicked.click();
+ } finally {
+ var index = wem.eventsPrevented.indexOf(targetId);
+ if (index !== -1) {
+ wem.eventsPrevented.splice(index, 1);
}
- }, function (xhr) {
- console.error('[WEM] Could not send click event.');
- if (errorCallback) {
- errorCallback(xhr);
- } else {
- target.click();
+ }
+ };
+ var once = settleOnce();
+ /**
+ * Once the event is sent, or could not be: the site's
callback, or the held back click
+ * @param {function(): void} log logs the outcome
+ * @param {Function} [callback] the site's callback for this
outcome
+ * @returns {function(XMLHttpRequest=): void} the request
callback, which only runs if nothing else settled
+ */
+ var whenDone = function (log, callback) {
+ /**
+ * @param {XMLHttpRequest} [xhr] the request
+ * @returns {undefined}
+ */
+ var settle = function (xhr) {
+ log();
+ if (callback) {
+ callback(xhr);
+ } else if (holdBack) {
+ dispatchAgain();
}
- });
+ };
+ return once(settle);
+ };
+ var onSent = whenDone(function () {
+ console.debug('[WEM] Click event successfully collected.');
+ }, successCallback);
+ var onNotSent = whenDone(function () {
+ console.error('[WEM] Could not send click event.');
+ }, errorCallback);
+
+ if (holdBack && wem.timeoutInMilliseconds === -1) {
+ setTimeout(whenDone(function () {
+ console.warn('[WEM] No answer from Unomi yet, the
click goes on.');
+ }, errorCallback), DEFAULT_TIMEOUT_IN_MILLISECONDS);
+ }
+ try {
+ wem.collectEvent(clickEvent, onSent, onNotSent);
+ } catch (e) {
+ // Go on once the visitor's click is done: a click
dispatched during it can be ignored
+ console.error('[WEM] Could not send click event:', e);
+ setTimeout(function () {
+ onNotSent(undefined);
+ }, 0);
}
}
},
@@ -1261,35 +1476,84 @@ export const newTracker = () => {
var form = event.target;
var formName = form.getAttribute('name') ?
form.getAttribute('name') : form.getAttribute('id');
if (formName && wem.formNamesToWatch.indexOf(formName) > -1) {
+ // Tracking is off, or Unomi could not be reached: nothing is
sent, so leave the submit alone
+ if (wem.fallback) {
+ return;
+ }
console.debug('[WEM] catching form ' + formName);
- var eventCopy = document.createEvent('Event');
- // Define that the event name is 'build'.
- eventCopy.initEvent('submit', event.bubbles, event.cancelable);
+ // The button that submitted the form, if any: the submitted
data must include it
+ var submitter = /** @type {HTMLButtonElement |
HTMLInputElement | null} */(event.submitter || null);
+
+ // A submit the site already prevented (it sends the form
itself), or into another tab
+ // or window: the page stays, so only send the event
+ var formTarget = (submitter &&
submitter.getAttribute('formtarget')) || form.getAttribute('target');
+ if (event.defaultPrevented || !opensInThisPage(formTarget)) {
+ try {
+ wem.collectEvent(wem.buildFormEvent(formName, form));
+ } catch (e) {
+ console.error('[WEM] Could not send form event:', e);
+ }
+ return;
+ }
event.stopImmediatePropagation();
event.preventDefault();
- wem.collectEvent(wem.buildFormEvent(formName, form),
- function () {
- form.removeEventListener('submit',
wem._formSubmitEventListener, true);
- form.dispatchEvent(eventCopy);
- if (!eventCopy.defaultPrevented &&
!eventCopy.cancelBubble) {
- form.submit();
+ // Once the event is sent, or could not be: submit the form
again, without the tracker's
+ // listener. The site's handlers see that submit, and it does
not go on if one of them
+ // prevents it. The HTMLFormElement methods are called
directly, as a field named
+ // "submit" or "requestSubmit" hides the form's own.
+ var continueSubmit = function () {
+ form.removeEventListener('submit',
wem._formSubmitEventListener, true);
+ try {
+ // As a native submit: the submit event, then the data
with the button. It
+ // refuses a button that is no longer in the form (the
site removed or replaced
+ // it meanwhile), before doing anything: then submit
the form as older browsers do.
+ var submitted = false;
+ if (typeof HTMLFormElement.prototype.requestSubmit ===
'function') {
+ try {
+
HTMLFormElement.prototype.requestSubmit.call(form, submitter);
+ submitted = true;
+ } catch (e) {
+ console.debug('[WEM] requestSubmit refused the
form ' + formName + ', submitting it without: ' + e);
+ }
}
- form.addEventListener('submit',
wem._formSubmitEventListener, true);
- },
- function (xhr) {
- console.error('[WEM] Error while collecting form
event: ' + xhr.status + ' ' + xhr.statusText);
- xhr.abort();
- form.removeEventListener('submit',
wem._formSubmitEventListener, true);
- form.dispatchEvent(eventCopy);
- if (!eventCopy.defaultPrevented &&
!eventCopy.cancelBubble) {
- form.submit();
+ if (!submitted) {
+ submitWithoutRequestSubmit(form, submitter);
}
+ } catch (e) {
+ console.error('[WEM] Could not submit form ' +
formName + ':', e);
+ } finally {
form.addEventListener('submit',
wem._formSubmitEventListener, true);
}
- );
+ };
+ // Submit once: when the event is sent, when it could not be
(also when sending it
+ // throws), or, without a request timeout, after 1.5s at most
+ var once = settleOnce();
+ var onSent = once(continueSubmit);
+ var onNotSent = once(function (xhr) {
+ console.error('[WEM] Error while collecting form event' +
(xhr ? ': ' + xhr.status + ' ' + xhr.statusText : ''));
+ if (xhr) {
+ xhr.abort();
+ }
+ continueSubmit();
+ });
+ if (wem.timeoutInMilliseconds === -1) {
+ setTimeout(once(function () {
+ console.warn('[WEM] No answer from Unomi yet, the form
is submitted.');
+ continueSubmit();
+ }), DEFAULT_TIMEOUT_IN_MILLISECONDS);
+ }
+ try {
+ wem.collectEvent(wem.buildFormEvent(formName, form),
onSent, onNotSent);
+ } catch (e) {
+ // Go on once the visitor's submit is done: browsers
ignore a submit made during it
+ console.error('[WEM] Could not send form event:', e);
+ setTimeout(function () {
+ onNotSent(undefined);
+ }, 0);
+ }
}
},
@@ -1362,11 +1626,12 @@ export const newTracker = () => {
* @private
*/
_resolveId: function (id) {
- if (wem.digitalData.sourceLocalIdentifierMap) {
- var source =
Object.keys(wem.digitalData.sourceLocalIdentifierMap).filter(function (source) {
- return id.indexOf(source) > 0;
+ var map = /** @type {Record<string, string> | undefined}
*/(wem.digitalData.sourceLocalIdentifierMap);
+ if (map) {
+ var sources = Object.keys(map).filter(function (source) {
+ return id.indexOf(source) !== -1;
});
- return source ? id.replace(source,
wem.digitalData.sourceLocalIdentifierMap[source]) : id;
+ return sources.length > 0 ? id.replace(sources[0],
map[sources[0]]) : id;
}
return id;
},
diff --git a/test/servers/README.md b/test/servers/README.md
index 241ea54..72f9a6f 100644
--- a/test/servers/README.md
+++ b/test/servers/README.md
@@ -44,6 +44,35 @@ The command builds the tracker and compares its requests
with those of the publi
for each server, it starts the server, creates the scope `tracker-test`, runs
the tests and removes
the server with its data. It exits with an error if anything fails.
+Browsers
+--------
+The tests run in Chromium by default. `TRACKER_BROWSERS` chooses others,
comma-separated:
+`chromium`, `firefox`, and `webkit` (Playwright's build of Safari's engine).
Install them once with
+`npx playwright install firefox webkit`.
+
+```shell
+TRACKER_BROWSERS=chromium,firefox,webkit yarn test:servers unomi3
+```
+
+WebKit is not Safari: popup blocking, how long a click still counts as the
visitor's own, and
+cookie rules (Intelligent Tracking Prevention) belong to Safari. `yarn
test:safari` checks the
+tracker in the real Safari of a Mac, against a Unomi simulated in Node (no
Docker): links followed
+once the event is sent, also when the visitor clicks inside a link; new tabs
opened while Unomi is
+slow; Cmd+click; single-page application routers; site click handlers run
once; links followed
+when Unomi cannot be reached or never answers; forms submitted with their
button, in browsers with
+and without `requestSubmit`. `yarn test:safari --tracker <file>` runs the same
checks with another
+build, for example to compare with the previous release, and `--only <text>`
only the checks whose
+question contains the text.
+
+Once, on the Mac: `sudo safaridriver --enable`, then in Safari, Settings >
Advanced > Show
+features for web developers, and Develop > Allow Remote Automation.
+
+Safari drops automated clicks on a hidden page (display asleep, screen locked,
window hidden), so
+each check runs in a Safari session of its own, the display is kept awake
during the run, and the
+run stops with no result (exit code 2) if the page is hidden anyway. A click
the page did not get
+is clicked again, and a check whose clicks were dropped runs once more; both
are shown in the
+output. A failing check also shows what Unomi got, the page's address and the
tracker's logs.
+
Servers
-------
| Name | Server | Search engine |
@@ -70,6 +99,38 @@ For each server:
Non-empty `attributes` are sent as before; no default page schema accepts
them, so the server
drops the event, as with 1.5.0. Unomi 2.0 to 2.3 accept an empty one; 2.4
and later reject it
too (UNOMI-790).
+- Personalization: Unomi chooses the variant whose condition matches, and the
tracker displays it.
+ When Unomi cannot be reached, or does not answer within
`timeoutInMilliseconds`, the tracker
+ falls back and displays the fallback variant.
+- A configuration without `timeoutInMilliseconds` loads the context and sends
events (UNOMI-1043).
+- Clicks sent with `sendClickEvent` (UNOMI-1037), on a button and on a link,
as an event listener
+ and with callbacks: every click is stored, also a second click made before
the first event is
+ sent, and none twice. When the event cannot be sent, the error callback is
called, or a link is
+ still followed. The site's own click handler runs once per click on a
button, and single-page
+ application routers, on the document or on the link itself, handle a tracked
link once per
+ click without reloading the page (UNOMI-1046).
+- A click on a child of a tracked link or button (an image, an icon, some
text), with or without
+ its own id, is stored on the tracked element; the link waits for the event,
then is followed
+ (UNOMI-1047).
+- Links and forms do not wait for Unomi when they keep the page: a link to a
new tab, a download, a
+ form submitted into a new tab; Ctrl or Cmd+click opens a new tab, and the
click is still stored
+ (UNOMI-1041). They still work at once when Unomi could not be reached, when
tracking is turned
+ off (`_enableWem(false)`), or when the event cannot even be sent; without a
timeout, they wait
+ 1.5s at most for a Unomi that never answers (UNOMI-1040).
+- A form: the setup adds a rule on its submit, so Unomi returns it in the
tracked conditions and
+ the tracker watches it. On submit, the form event is stored with the field
values (text, text
+ area, checked checkboxes and radio button, selected options; empty fields
are left out). The
+ site's own submit handler runs once, once the event is sent or could not be,
and learns which
+ button was used. A form the site lets submit sends the same data as a native
submit, with the
+ button that was used (also one named `submit`, an image button, one the site
removes meanwhile),
+ in browsers with and without `requestSubmit` (UNOMI-1042), also with Enter,
when the event cannot
+ be sent, and at once when tracking is turned off (UNOMI-1040). A click on a
tracked submit button
+ of a tracked form stores the click and the form event.
+
+Not tested yet: optimization tests, search and video events,
`invalidateSessionAndProfile`,
+digitalData overrides, personalization with a control group or a result
callback, and a site and
+Unomi on different domains (the test origins are all on localhost, which
browsers treat as one
+site).
Files
-----
diff --git a/test/servers/page/index.html b/test/servers/page/index.html
index b7e35d8..8ba2f6f 100644
--- a/test/servers/page/index.html
+++ b/test/servers/page/index.html
@@ -42,16 +42,55 @@
// noBufferShim load the tracker without the Buffer shim (optional)
// pageAttributes digitalData.page.attributes as JSON, for example {}
(optional)
// sessionId session ID to use instead of a new one (optional)
+ // timeout wemInitConfig.timeoutInMilliseconds (optional, 10000
by default); "none" leaves it
+ // out of the configuration
+ // personalization "true": a personalized area with three variants
(optional)
+ // clickButton id of a button whose clicks are sent with
sendClickEvent (optional)
+ // clickLink the element is a link instead of a button: "true" to
/arrived; "hash" to #arrived,
+ // which keeps the page open; "blank" to /arrived in a
new tab; "download" to /arrived
+ // as a download (optional)
+ // clickChild "plain" or "withId": the visitor clicks a child of
the element, without or with
+ // its own id, as an image or an icon inside a link
(optional)
+ // clickCallback "true": the element passes success and error
callbacks to sendClickEvent (optional)
+ // form name of a form, watched by the tracker when Unomi
tracks it (optional)
+ // formSubmit "native": the site lets the form submit to
/submitted, instead of handling it (optional)
+ // router a stand-in for a single-page application router,
which handles the link clicks
+ // nobody prevented yet: "true" listens on the document
(React Router listens on the
+ // application's root), "link" on the link itself
before the tracker (as Vue Router's
+ // RouterLink) (optional)
+ // formTarget the form's target, for example "_blank" (optional)
+ // trackSubmitButton "true": the clicks on the form's "delete" button
are sent with sendClickEvent (optional)
+ // oldBrowser "true": no HTMLFormElement.requestSubmit nor
SubmitEvent, as in older browsers (optional)
(function () {
const params = new URLSearchParams(document.location.search);
+ if (params.get('oldBrowser') === 'true') {
+ delete HTMLFormElement.prototype.requestSubmit;
+ delete window.SubmitEvent;
+ }
+ // Changes with each page load: the same value after a click shows the
page was not reloaded
+ const pageLoadId = Math.random();
+ let routed = 0;
+ if (params.get('router') === 'true') {
+ document.addEventListener('click', function (event) {
+ const link = event.target.closest && event.target.closest('a');
+ if (link && !event.defaultPrevented) {
+ event.preventDefault();
+ history.pushState({}, '', link.getAttribute('href'));
+ routed++;
+ }
+ });
+ }
const wemInitConfig = {
contextServerUrl: params.get('unomi'),
- timeoutInMilliseconds: 10000,
+ timeoutInMilliseconds: params.has('timeout') ?
Number(params.get('timeout')) : 10000,
contextServerCookieName: 'context-profile-id',
activateWem: true,
trackerSessionIdCookieName: 'tracker-test-session-id',
trackerProfileIdCookieName: 'tracker-test-profile-id'
};
+ if (params.get('timeout') === 'none') {
+ delete wemInitConfig.timeoutInMilliseconds;
+ }
const digitalData = {
scope: 'tracker-test',
site: { siteInfo: { siteID: 'tracker-test' } },
@@ -85,7 +124,174 @@
// initTracker replaces the tracker configuration, so register the
callback after it
tracker.initTracker(digitalData);
+ // A personalized area: Unomi picks "always" (its condition always
matches) over "never" (its
+ // condition never matches); "fallback" is shown when Unomi cannot be
reached.
+ const personalizedArea = document.createElement('div');
+ if (params.get('personalization') === 'true') {
+ personalizedArea.id = 'tracker-test-personalization';
+ ['never', 'always', 'fallback'].forEach(function (name) {
+ const variant = document.createElement('div');
+ variant.id = 'variant-' + name;
+ variant.textContent = name;
+ personalizedArea.appendChild(variant);
+ });
+ document.body.appendChild(personalizedArea);
+ tracker.registerPersonalizationObject({
+ id: personalizedArea.id,
+ strategy: 'matching-first',
+ strategyOptions: { fallback: 'fallback' },
+ contents: [
+ { id: 'never', filters: [{ condition: { type:
'profilePropertyCondition', parameterValues: { propertyName:
'properties.trackerTestNever', comparisonOperator: 'exists' } } }] },
+ { id: 'always', filters: [{ condition: { type:
'matchAllCondition', parameterValues: {} } }] }
+ ]
+ }, {
+ never: { content: 'variant-never', position: 0 },
+ always: { content: 'variant-always', position: 1 },
+ fallback: { content: 'variant-fallback', position: 2 }
+ });
+ }
+
+ // A button or a link sending its clicks the way the Unomi manual
shows, or with callbacks, as a
+ // site does when it handles the click itself once the event is sent
+ const clickCallbacks = { success: 0, error: 0 };
+ let siteClicks = 0;
+ if (params.has('clickButton')) {
+ const element = document.createElement(params.has('clickLink') ?
'a' : 'button');
+ element.id = params.get('clickButton');
+ if (element.tagName === 'A') {
+ const kind = params.get('clickLink');
+ element.href = kind === 'hash' ? '#arrived' : '/arrived';
+ if (kind === 'blank') {
+ element.target = '_blank';
+ } else if (kind === 'download') {
+ element.setAttribute('download', 'arrived.html');
+ }
+ } else {
+ element.type = 'button';
+ }
+ element.textContent = 'Tracked element';
+ if (params.has('clickChild')) {
+ const child = document.createElement('span');
+ child.className = 'tracked-child';
+ child.textContent = ' (child)';
+ if (params.get('clickChild') === 'withId') {
+ child.id = element.id + '-child';
+ }
+ element.appendChild(child);
+ }
+ if (params.get('router') === 'link') {
+ element.addEventListener('click', function (event) {
+ if (!event.defaultPrevented) {
+ event.preventDefault();
+ history.pushState({}, '',
element.getAttribute('href'));
+ routed++;
+ }
+ });
+ }
+ // The site's own click handler, registered before the tracker's
+ element.addEventListener('click', function () {
+ siteClicks++;
+ });
+ if (params.get('clickCallback') === 'true') {
+ element.addEventListener('click', function (event) {
+ tracker.sendClickEvent(event, function () {
+ clickCallbacks.success++;
+ }, function () {
+ clickCallbacks.error++;
+ });
+ });
+ } else {
+ element.addEventListener('click', tracker.sendClickEvent);
+ }
+ document.body.appendChild(element);
+ }
+
+ // A form with the usual fields, several submit buttons (one named
"submit", one the site removes
+ // after the click) and an image button. The site handles the submit
itself (like
+ // a form sent with JavaScript), so the page stays open, or with
formSubmit=native lets it go to
+ // /submitted. The tracker stops the visitor's own submit and sends
the form event; the site's
+ // handler then only gets the submit the tracker makes once the event
is sent.
+ const siteSubmits = [];
+ if (params.has('form')) {
+ const form = document.createElement('form');
+ form.setAttribute('name', params.get('form'));
+ form.action = '/submitted';
+ form.method = 'get';
+ form.innerHTML = '<input type="email" name="email"
value="[email protected]">'
+ + '<input type="checkbox" name="topics" value="news" checked>'
+ + '<input type="checkbox" name="topics" value="offers">'
+ + '<select name="country"><option
value="fr">France</option><option value="ch"
selected>Switzerland</option></select>'
+ + '<select name="languages" multiple><option value="fr"
selected>French</option><option value="de" selected>German</option><option
value="it">Italian</option></select>'
+ + '<input type="radio" name="contact" value="email"
checked><input type="radio" name="contact" value="phone">'
+ + '<textarea name="message">Hello</textarea>'
+ + '<input type="text" name="company" value="">'
+ + '<button type="submit" id="submit-form" name="action"
value="send">Send</button>'
+ + '<button type="submit" id="delete-form" name="action"
value="delete">Delete</button>'
+ // A button named "submit" hides the form's own submit()
+ + '<button type="submit" id="submit-named" name="submit"
value="go">Go</button>'
+ // The site takes this button out of the form right after the
click
+ + '<button type="submit" id="submit-removed" name="action"
value="removed">Done</button>'
+ + '<input type="image" id="submit-image" name="pic" alt="Send"
width="20" height="20"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">';
+ form.addEventListener('submit', function (event) {
+ if (params.get('formSubmit') !== 'native') {
+ event.preventDefault();
+ }
+ siteSubmits.push({ submitter: event.submitter ?
event.submitter.id : null });
+ });
+ if (params.has('formTarget')) {
+ form.target = params.get('formTarget');
+ }
+ if (params.get('trackSubmitButton') === 'true') {
+ form.querySelector('#delete-form').addEventListener('click',
tracker.sendClickEvent);
+ }
+ form.querySelector('#submit-removed').addEventListener('click',
function (event) {
+ setTimeout(function () {
+ event.target.remove();
+ }, 0);
+ });
+ document.body.appendChild(form);
+ }
+
window.trackerTest = {
+ // The variants of the personalized area that are displayed
+ shownVariants: function () {
+ return Array.prototype.filter.call(personalizedArea.children,
function (variant) {
+ return variant.style.display !== 'none';
+ }).map(function (variant) {
+ return variant.id;
+ });
+ },
+ // The submits the site's own handler received
+ siteSubmits: function () {
+ return siteSubmits;
+ },
+ // Whether the page is still the one first loaded, and how many
link clicks the router handled
+ pageLoadId: function () {
+ return pageLoadId;
+ },
+ routed: function () {
+ return routed;
+ },
+ // How many times the site's own click handler ran on the element
+ siteClicks: function () {
+ return siteClicks;
+ },
+ // How many times sendClickEvent called the element's success and
error callbacks
+ clickCallbacks: function () {
+ return clickCallbacks;
+ },
+ // Makes sending the next events throw at once (the request cannot
even be opened)
+ breakServerUrl: function () {
+ tracker.contextServerUrl = 'http://[';
+ },
+ // Turns tracking off, as a site does when the visitor withdraws
consent
+ disableTracking: function () {
+ tracker._enableWem(false);
+ },
+ // The forms the tracker watches (those Unomi returned in the
tracked conditions)
+ watchedForms: function () {
+ return tracker.getFormNamesToWatch();
+ },
// Resolves once the context is loaded, or once the tracker falls
back after an error
context: new Promise(function (resolve) {
tracker._registerCallback(function () {
diff --git a/test/servers/playwright.config.js
b/test/servers/playwright.config.js
index 579b376..692ac77 100644
--- a/test/servers/playwright.config.js
+++ b/test/servers/playwright.config.js
@@ -17,9 +17,24 @@
// Browser tests of the tracker against one running Unomi server. Run them
through
// `yarn test:servers`, which starts each server and sets UNOMI_TARGET.
+// TRACKER_BROWSERS chooses the browsers, comma-separated: chromium (the
default), firefox, webkit
+// (Playwright's build of Safari's engine).
const path = require('path');
const { defineConfig } = require('@playwright/test');
+const BROWSERS = {
+ // The tracker treats a "HeadlessChrome" user agent as a crawler and does
not start
+ chromium: { browserName: 'chromium', userAgent: 'Mozilla/5.0 (Macintosh;
Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0
Safari/537.36' },
+ firefox: { browserName: 'firefox' },
+ webkit: { browserName: 'webkit' }
+};
+const browsers = (process.env.TRACKER_BROWSERS ||
'chromium').split(',').map(name => name.trim()).filter(Boolean);
+for (const name of browsers) {
+ if (!BROWSERS[name]) {
+ throw new Error('Unknown browser "' + name + '" in TRACKER_BROWSERS,
expected: ' + Object.keys(BROWSERS).join(', '));
+ }
+}
+
module.exports = defineConfig({
testDir: __dirname,
testMatch: 'tracker.spec.js',
@@ -28,11 +43,7 @@ module.exports = defineConfig({
workers: 1,
reporter: 'list',
globalSetup: require.resolve('./global-setup.js'),
- use: {
- browserName: 'chromium',
- // The tracker treats a "HeadlessChrome" user agent as a crawler and
does not start
- userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'
- },
+ projects: browsers.map(name => ({ name, use: BROWSERS[name] })),
webServer: {
command: 'node ' + JSON.stringify(path.join(__dirname, 'serve.js')),
url: 'http://localhost:8000/',
diff --git a/test/servers/safari.js b/test/servers/safari.js
new file mode 100644
index 0000000..019f527
--- /dev/null
+++ b/test/servers/safari.js
@@ -0,0 +1,475 @@
+/*
+ * 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.
+ */
+
+// Checks the tracker in the real Safari of this Mac, where Playwright's
WebKit can differ (popup
+// blocking, how long a click counts as the visitor's own, cookies). Safari is
driven through
+// safaridriver (WebDriver), whose clicks are real user clicks. Unomi is
simulated here, so no
+// Docker is needed: it can answer, answer late, never answer, or refuse
connections, and it
+// counts the events it gets.
+//
+// yarn test:safari the current build (dist), built
first
+// yarn test:safari --tracker <file> another build of
apache-unomi-tracker.umd.js
+// yarn test:safari --only <text> only the checks whose question
contains the text
+//
+// Once, on the Mac: `sudo safaridriver --enable`, then in Safari, Settings >
Advanced > Show
+// features for web developers, and Develop > Allow Remote Automation.
+const http = require('http');
+const path = require('path');
+const fs = require('fs');
+const { spawn, spawnSync } = require('child_process');
+
+const DRIVER_PORT = 4445;
+const UNOMI_PORT = 8282;
+const UNOMI = 'http://localhost:' + UNOMI_PORT;
+const SITE = 'http://localhost:8000';
+const FORM_NAME = 'tracker-test-form';
+const ELEMENT = 'element-6066-11e4-a52e-4f735466cecf';
+const KEYS = { meta: '' };
+
+const args = process.argv.slice(2);
+const trackerArg = args.indexOf('--tracker');
+const trackerFile = trackerArg !== -1 ? path.resolve(args[trackerArg + 1]) :
null;
+const onlyArg = args.indexOf('--only');
+const only = onlyArg !== -1 ? args[onlyArg + 1].toLowerCase() : null;
+
+// --- Simulated Unomi
-------------------------------------------------------------------------
+
+const unomi = { mode: 'answer', delayMs: 0, events: [], requests: [] };
+
+function corsHeaders(request) {
+ return {
+ 'Access-Control-Allow-Origin': request.headers.origin || '*',
+ 'Access-Control-Allow-Credentials': 'true',
+ 'Access-Control-Allow-Headers': 'Content-Type, X-Unomi-Api-Key',
+ 'Access-Control-Allow-Methods': 'POST, OPTIONS'
+ };
+}
+
+const unomiServer = http.createServer((request, response) => {
+ if (request.method === 'OPTIONS') {
+ response.writeHead(204, corsHeaders(request)).end();
+ return;
+ }
+ let body = '';
+ request.on('data', chunk => body += chunk);
+ request.on('end', () => {
+ const url = new URL(request.url, UNOMI);
+ unomi.requests.push(request.method + ' ' + url.pathname +
(url.pathname === '/eventcollector' ? ' (' + unomi.mode + ')' : ''));
+ if (url.pathname === '/context.json') {
+ response.writeHead(200, Object.assign({ 'Content-Type':
'application/json' }, corsHeaders(request)));
+ response.end(JSON.stringify({
+ profileId: 'safari-profile',
+ sessionId: 'safari-session',
+ trackedConditions: [{ type: 'formEventCondition',
parameterValues: { formId: FORM_NAME } }]
+ }));
+ return;
+ }
+ if (url.pathname === '/eventcollector') {
+ if (unomi.mode === 'refuse') {
+ request.socket.destroy();
+ return;
+ }
+ if (unomi.mode === 'never') {
+ return;
+ }
+ const answer = () => {
+ try {
+ unomi.events.push(...JSON.parse(body).events);
+ } catch (e) {
+ // not an event list
+ }
+ response.writeHead(200, Object.assign({ 'Content-Type':
'application/json' }, corsHeaders(request)));
+ response.end('{}');
+ };
+ setTimeout(answer, unomi.delayMs);
+ return;
+ }
+ response.writeHead(404, corsHeaders(request)).end();
+ });
+});
+
+function setUnomi(mode, delayMs = 0) {
+ unomi.mode = mode;
+ unomi.delayMs = delayMs;
+ unomi.events = [];
+ unomi.requests = [];
+}
+
+// --- WebDriver
---------------------------------------------------------------------------------
+
+let session;
+// The clicks Safari dropped, clicked again
+const retriedClicks = [];
+
+async function driver(method, endpoint, body) {
+ const response = await fetch('http://localhost:' + DRIVER_PORT + endpoint,
{
+ method,
+ headers: { 'Content-Type': 'application/json' },
+ body: body === undefined ? undefined : JSON.stringify(body)
+ });
+ const result = await response.json();
+ if (!response.ok) {
+ throw new Error(method + ' ' + endpoint + ': ' +
JSON.stringify(result.value));
+ }
+ return result.value;
+}
+
+const safari = {
+ open: url => driver('POST', '/session/' + session + '/url', { url }),
+ url: () => driver('GET', '/session/' + session + '/url'),
+ run: (script, ...scriptArgs) => driver('POST', '/session/' + session +
'/execute/sync', { script, args: scriptArgs }),
+ runAsync: (script, ...scriptArgs) => driver('POST', '/session/' + session
+ '/execute/async', { script, args: scriptArgs }),
+ find: async selector => (await driver('POST', '/session/' + session +
'/element', { using: 'css selector', value: selector }))[ELEMENT],
+ clickOnce: async selector => driver('POST', '/session/' + session +
'/element/' + await safari.find(selector) + '/click', {}),
+ // Safari sometimes drops a WebDriver click (after a tab was closed, the
page may not have the
+ // focus yet): click again if the page saw no click and did not change,
and say so
+ click: async selector => {
+ for (let attempt = 1; ; attempt++) {
+ const before = await safari.url();
+ const clicksBefore = await safari.run('return window.harnessClicks
|| 0;');
+ await safari.clickOnce(selector);
+ const delivered = await waitFor(async () => await safari.url() !==
before || await safari.run('return window.harnessClicks || 0;') > clicksBefore,
1000);
+ if (delivered || attempt === 2) {
+ return;
+ }
+ // Whether the page had the focus and was visible: Safari drops
clicks when it has not
+ const state = await safari.run('return (document.hasFocus() ?
"focused" : "not focused") + ", " + document.visibilityState;');
+ retriedClicks.push(selector + ' (page ' + state + ')');
+ }
+ },
+ clickWithKey: async (selector, key) => {
+ const element = await safari.find(selector);
+ await driver('POST', '/session/' + session + '/actions', {
+ actions: [
+ { type: 'key', id: 'keyboard', actions: [{ type: 'keyDown',
value: key }, { type: 'pause', duration: 0 }, { type: 'pause', duration: 0 }, {
type: 'keyUp', value: key }] },
+ { type: 'pointer', id: 'mouse', parameters: { pointerType:
'mouse' }, actions: [{ type: 'pointerMove', origin: { [ELEMENT]: element }, x:
0, y: 0 }, { type: 'pointerDown', button: 0 }, { type: 'pointerUp', button: 0
}, { type: 'pause', duration: 0 }] }
+ ]
+ });
+ await driver('DELETE', '/session/' + session + '/actions');
+ },
+ windows: () => driver('GET', '/session/' + session + '/window/handles'),
+ switchTo: handle => driver('POST', '/session/' + session + '/window', {
handle }),
+ closeWindow: () => driver('DELETE', '/session/' + session + '/window')
+};
+
+async function closeOtherWindows(keep) {
+ for (const handle of await safari.windows()) {
+ if (handle !== keep) {
+ await safari.switchTo(handle);
+ await safari.closeWindow();
+ }
+ }
+ await safari.switchTo(keep);
+}
+
+// --- Checks
------------------------------------------------------------------------------------
+
+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
+
+class PageHidden extends Error {
+ constructor() {
+ super('Safari\'s page is hidden: the display is asleep, the screen is
locked, or the window cannot be seen');
+ }
+}
+
+async function waitFor(condition, timeoutMs) {
+ const deadline = Date.now() + timeoutMs;
+ for (;;) {
+ const value = await condition();
+ if (value || Date.now() > deadline) {
+ return value;
+ }
+ await sleep(100);
+ }
+}
+
+// Opens the test page with the given options and waits for the tracker to
load the context. It
+// starts from a blank page, and waits for the test page itself: a navigation
left over from the
+// previous check (a link followed late) must not land on this one.
+async function openPage(params) {
+ await safari.open('about:blank');
+ await sleep(500);
+ const query = new URLSearchParams(Object.assign({ unomi: UNOMI, sessionId:
'safari-session-' + Date.now() }, params));
+ const url = SITE + '/?' + query;
+ await safari.open(url);
+ const ready = await waitFor(async () => await safari.url() === url &&
await safari.run('return !!(window.trackerTest &&
window.trackerTest.context);'), 5000);
+ if (!ready) {
+ throw new Error('The test page did not load: at ' + await
safari.url());
+ }
+ // Count the clicks the page sees, and keep what the tracker logs from now
on, to explain a failure
+ await safari.run(`window.harnessClicks = 0;
+ window.addEventListener('click', () => window.harnessClicks++, true);
+ window.trackerLogs = [];
+ ['debug', 'info', 'log', 'warn', 'error'].forEach(level => {
+ const original = console[level];
+ console[level] = function () {
+ window.trackerLogs.push(level + ': ' +
Array.prototype.join.call(arguments, ' '));
+ return original.apply(console, arguments);
+ };
+ });`);
+ // Let Safari give the page the focus before the first click. Safari drops
the clicks on a
+ // hidden page (display asleep, screen locked, window hidden): that is no
tracker result.
+ await sleep(300);
+ if (await safari.run('return document.visibilityState;') === 'hidden') {
+ throw new PageHidden();
+ }
+ return safari.runAsync('const done = arguments[arguments.length - 1];
window.trackerTest.context.then(done);');
+}
+
+const pathOf = async () => new URL(await safari.url()).pathname;
+const eventsOn = targetId => unomi.events.filter(event => event.target &&
event.target.itemId === targetId);
+
+const checks = [
+ {
+ question: 'A tracked link is followed once its event is sent',
+ async run() {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'link', clickLink: 'true' });
+ await safari.click('#link');
+ const arrived = await waitFor(async () => await pathOf() ===
'/arrived', 5000);
+ return { ok: arrived && eventsOn('link').length === 1, details:
'followed: ' + arrived + ', events: ' + eventsOn('link').length };
+ }
+ },
+ {
+ question: 'A click on an image or text inside a tracked link is
tracked, and the link followed',
+ async run() {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'link', clickLink: 'true',
clickChild: 'plain' });
+ await safari.click('#link .tracked-child');
+ const arrived = await waitFor(async () => await pathOf() ===
'/arrived', 5000);
+ return { ok: arrived && eventsOn('link').length === 1, details:
'followed: ' + arrived + ', events on the link: ' + eventsOn('link').length };
+ }
+ },
+ {
+ question: 'A link to a new tab opens it while Unomi is slow (3s), with
no popup blocked',
+ async run(main) {
+ setUnomi('answer', 3000);
+ await openPage({ clickButton: 'blank', clickLink: 'blank' });
+ const before = (await safari.windows()).length;
+ await safari.click('#blank');
+ const opened = await waitFor(async () => (await
safari.windows()).length > before, 5000);
+ await waitFor(async () => eventsOn('blank').length === 1, 5000);
+ await closeOtherWindows(main);
+ return { ok: opened && eventsOn('blank').length === 1, details:
'new tab: ' + opened + ', events: ' + eventsOn('blank').length };
+ }
+ },
+ {
+ question: 'Cmd+click on a tracked link opens a new tab and keeps the
page',
+ async run(main) {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'link', clickLink: 'true' });
+ const before = (await safari.windows()).length;
+ await safari.clickWithKey('#link', KEYS.meta);
+ const opened = await waitFor(async () => (await
safari.windows()).length > before, 3000);
+ const stayed = await pathOf() === '/';
+ await waitFor(async () => eventsOn('link').length === 1, 3000);
+ await closeOtherWindows(main);
+ return { ok: opened && stayed && eventsOn('link').length === 1,
details: 'new tab: ' + opened + ', page kept: ' + stayed + ', events: ' +
eventsOn('link').length };
+ }
+ },
+ {
+ question: 'A single-page application router (on the document) handles
a tracked link without a reload',
+ async run() {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'link', clickLink: 'true', router:
'true' });
+ const loadId = await safari.run('return
window.trackerTest.pageLoadId();');
+ await safari.click('#link');
+ const routed = await waitFor(() => safari.run('return
window.trackerTest.routed();'), 5000);
+ const sameLoad = await safari.run('return
window.trackerTest.pageLoadId();') === loadId;
+ return { ok: routed === 1 && sameLoad, details: 'routed: ' +
routed + ', no reload: ' + sameLoad };
+ }
+ },
+ {
+ question: 'A router on the link itself (as Vue Router) handles each
click once',
+ async run() {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'link', clickLink: 'true', router:
'link' });
+ await safari.click('#link');
+ await waitFor(async () => eventsOn('link').length === 1, 3000);
+ await sleep(700);
+ const routed = await safari.run('return
window.trackerTest.routed();');
+ return { ok: routed === 1, details: 'routed: ' + routed };
+ }
+ },
+ {
+ question: 'A site click handler on a tracked button runs once per
click',
+ async run() {
+ setUnomi('answer', 300);
+ await openPage({ clickButton: 'button' });
+ await safari.click('#button');
+ await waitFor(async () => eventsOn('button').length === 1, 3000);
+ await sleep(500);
+ const handled = await safari.run('return
window.trackerTest.siteClicks();');
+ return { ok: handled === 1, details: 'site handler runs: ' +
handled };
+ }
+ },
+ {
+ question: 'A link is followed at once when Unomi could not be reached',
+ async run() {
+ setUnomi('refuse');
+ const context = await openPage({ clickButton: 'link', clickLink:
'true', unomi: 'http://localhost:1' });
+ await safari.click('#link');
+ const arrived = await waitFor(async () => await pathOf() ===
'/arrived', 3000);
+ return { ok: !context.loaded && arrived, details: 'context loaded:
' + context.loaded + ', followed: ' + arrived };
+ }
+ },
+ {
+ question: 'A link is followed within 1.5s when Unomi never answers and
there is no timeout',
+ async run() {
+ setUnomi('never');
+ await openPage({ clickButton: 'link', clickLink: 'true', timeout:
'-1' });
+ const start = Date.now();
+ await safari.click('#link');
+ const arrived = await waitFor(async () => await pathOf() ===
'/arrived', 5000);
+ const elapsed = Date.now() - start;
+ return { ok: arrived && elapsed >= 1400, details: 'followed: ' +
arrived + ' after ' + elapsed + 'ms' };
+ }
+ },
+ ...[
+ { name: 'its button', button: 'delete-form', expect: { action:
'delete' } },
+ { name: 'a button named "submit"', button: 'submit-named', expect: {
submit: 'go' } },
+ { name: 'an image button', button: 'submit-image', expect: { 'pic.x':
/^\d+$/ } }
+ ].flatMap(form => [false, true].map(oldBrowser => ({
+ question: 'A tracked form is submitted with ' + form.name +
(oldBrowser ? ', without requestSubmit' : ''),
+ async run() {
+ setUnomi('answer', 300);
+ await openPage(Object.assign({ form: FORM_NAME, formSubmit:
'native' }, oldBrowser ? { oldBrowser: 'true' } : {}));
+ await safari.click('#' + form.button);
+ const submitted = await waitFor(async () => await pathOf() ===
'/submitted', 5000);
+ const sent = new URL(await safari.url()).searchParams;
+ const fields = Object.keys(form.expect).every(name => {
+ const value = sent.get(name);
+ return value !== null && (form.expect[name] instanceof RegExp
? form.expect[name].test(value) : value === form.expect[name]);
+ });
+ const formEvents = unomi.events.filter(event => event.eventType
=== 'form').length;
+ return { ok: submitted && fields && formEvents === 1, details:
'submitted: ' + submitted + ', ' + sent.toString() + ', form events: ' +
formEvents };
+ }
+ }))),
+ {
+ question: 'Safari has requestSubmit and SubmitEvent',
+ async run() {
+ await openPage({});
+ const support = await safari.run('return { requestSubmit: typeof
HTMLFormElement.prototype.requestSubmit, SubmitEvent: typeof SubmitEvent };');
+ return { ok: support.requestSubmit === 'function' &&
support.SubmitEvent === 'function', details: JSON.stringify(support) };
+ }
+ }
+];
+
+// --- Run
---------------------------------------------------------------------------------------
+
+async function main() {
+ const root = path.join(__dirname, '..', '..');
+ if (!trackerFile && spawnSync('yarn', ['build'], { cwd: root, stdio:
'inherit' }).status !== 0) {
+ process.exit(1);
+ }
+ const env = Object.assign({}, process.env, trackerFile ? { TRACKER_FILE:
trackerFile } : {});
+ const page = spawn('node', [path.join(__dirname, 'serve.js')], { env,
stdio: 'ignore' });
+ const safaridriver = spawn('/usr/bin/safaridriver', ['-p',
String(DRIVER_PORT)], { stdio: 'ignore' });
+ await new Promise(resolve => unomiServer.listen(UNOMI_PORT, resolve));
+ const stop = () => {
+ page.kill();
+ safaridriver.kill();
+ unomiServer.close();
+ if (awake) {
+ awake.kill();
+ }
+ };
+ // Keep the display awake, and wake it, while the checks run: Safari drops
clicks on a hidden page
+ const awake = fs.existsSync('/usr/bin/caffeinate') ?
spawn('/usr/bin/caffeinate', ['-d', '-u'], { stdio: 'ignore' }) : null;
+ try {
+ await sleep(1500);
+ // Each check in a session of its own: a tab a check opened and closed
can leave Safari's window
+ // without the focus, and Safari then drops the clicks of the
following checks
+ const startSession = async () => {
+ session = (await driver('POST', '/session', { capabilities: {
alwaysMatch: { browserName: 'safari' } } })).sessionId;
+ return (await safari.windows())[0];
+ };
+ try {
+ await startSession();
+ await driver('DELETE', '/session/' + session);
+ } catch (e) {
+ console.error('Could not start Safari through safaridriver: ' +
e.message);
+ console.error('Once: `sudo safaridriver --enable`, then Safari >
Develop > Allow Remote Automation.');
+ process.exitCode = 2;
+ return;
+ }
+ console.log('Tracker: ' + (trackerFile ||
'dist/apache-unomi-tracker.umd.js') + '\n');
+ let failed = 0;
+ const selected = checks.filter(check => !only ||
check.question.toLowerCase().includes(only));
+ for (const check of selected) {
+ let result;
+ let rerun = false;
+ // A check whose clicks Safari dropped tells nothing about the
tracker: run it once more
+ for (let attempt = 1; attempt <= 2; attempt++) {
+ if (attempt === 2) {
+ await driver('DELETE', '/session/' + session);
+ rerun = true;
+ }
+ retriedClicks.length = 0;
+ const mainWindow = await startSession();
+ try {
+ result = await check.run(mainWindow);
+ } catch (e) {
+ if (e instanceof PageHidden) {
+ throw e;
+ }
+ result = { ok: false, details: e.message };
+ }
+ if (result.ok || !retriedClicks.length) {
+ break;
+ }
+ }
+ failed += result.ok ? 0 : 1;
+ console.log((result.ok ? 'PASS ' : 'FAIL ') + check.question + '\n
' + result.details);
+ if (rerun) {
+ console.log(' (run again: Safari dropped the clicks of the
first run)');
+ }
+ if (retriedClicks.length) {
+ console.log(' (Safari dropped a click on ' +
retriedClicks.join(', ') + ', clicked again)');
+ }
+ if (!result.ok) {
+ // What happened, to tell a tracker problem from a test or
browser one
+ console.log(' Unomi got: ' + (unomi.requests.join(', ') ||
'nothing'));
+ try {
+ console.log(' page: ' + await safari.url());
+ const logs = await safari.run('return window.trackerLogs
|| null;');
+ console.log(' tracker logs: ' + (logs ?
logs.filter(line => line.includes('[WEM]')).join(' | ') || 'none' : 'not on
this page (it was left)'));
+ } catch (e) {
+ console.log(' page: unavailable (' + e.message + ')');
+ }
+ }
+ await driver('DELETE', '/session/' + session);
+ }
+ console.log('\n' + (selected.length - failed) + ' of ' +
selected.length + ' passed');
+ process.exitCode = failed ? 1 : 0;
+ } finally {
+ stop();
+ }
+}
+
+if (!fs.existsSync('/usr/bin/safaridriver')) {
+ console.error('safaridriver not found: these checks run on macOS, with
Safari');
+ process.exit(2);
+}
+main().catch(e => {
+ if (e instanceof PageHidden) {
+ console.error('\n' + e.message + '. The checks need a visible Safari
window: stopped, no result.');
+ process.exit(2);
+ }
+ console.error(e);
+ process.exit(1);
+});
diff --git a/test/servers/serve.js b/test/servers/serve.js
index f702eb4..45f4166 100644
--- a/test/servers/serve.js
+++ b/test/servers/serve.js
@@ -24,13 +24,24 @@ const path = require('path');
const PORTS = [8000, 8001];
const files = {
'/': { file: path.join(__dirname, 'page', 'index.html'), type: 'text/html;
charset=utf-8' },
- '/tracker.js': { file: path.join(__dirname, '..', '..', 'dist',
'apache-unomi-tracker.umd.js'), type: 'text/javascript; charset=utf-8' },
+ // The current build, or another one given in TRACKER_FILE (safari.js
--tracker)
+ '/tracker.js': { file: process.env.TRACKER_FILE || path.join(__dirname,
'..', '..', 'dist', 'apache-unomi-tracker.umd.js'), type: 'text/javascript;
charset=utf-8' },
// Downloaded by run.js, for the comparison with the published 1.5.0
'/tracker-1.5.0.js': { file: path.join(__dirname, '.cache',
'apache-unomi-tracker-1.5.0', 'package', 'dist',
'apache-unomi-tracker.umd.js'), type: 'text/javascript; charset=utf-8' }
};
+// Where the test page's links and forms lead: a plain page (Firefox shows its
own error page for
+// an empty answer, and the tests then cannot see the address)
+const DESTINATIONS = ['/arrived', '/submitted'];
+
function handle(request, response) {
- const entry = files[new URL(request.url, 'http://localhost').pathname];
+ const pathname = new URL(request.url, 'http://localhost').pathname;
+ if (DESTINATIONS.includes(pathname)) {
+ response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-store' })
+ .end('<!DOCTYPE html><title>Arrived</title><p>Arrived</p>');
+ return;
+ }
+ const entry = files[pathname];
if (!entry) {
response.writeHead(404).end();
return;
diff --git a/test/servers/tracker.spec.js b/test/servers/tracker.spec.js
index 36d3a2c..b822cac 100644
--- a/test/servers/tracker.spec.js
+++ b/test/servers/tracker.spec.js
@@ -16,7 +16,7 @@
*/
const { test, expect } = require('@playwright/test');
-const { UNOMI_URL, target, findClickEvents } = require('./unomi');
+const { UNOMI_URL, FORM_NAME, target, findEvents } = require('./unomi');
const server = target(process.env.UNOMI_TARGET);
const LISTED_ORIGIN = 'http://localhost:8000';
@@ -41,20 +41,70 @@ async function openPage(page, origin, config, timeoutMs =
30000) {
return { context, requests };
}
+// An id no earlier test or run has used, so that the events found on it are
this test's
+function uniqueId(label) {
+ return label + '-' + Date.now() + '-' + Math.floor(Math.random() * 1e6);
+}
+
+// Only the events of the page that loaded this context: a server kept from an
earlier run can hold
+// older ones on the same target
+function thisSession(context) {
+ return event => event.sessionId === context.sessionId;
+}
+
+// The events stored on the target, with the credentials that can read them
+function events(targetId, options) {
+ return findEvents(process.env.UNOMI_READ_CREDENTIALS, targetId, options);
+}
+
+// The form fields of the submitted page's address, as a native submit sends
them: the form's
+// fields, plus only the button that was used, with the expected values
(strings or patterns)
+function expectSubmitted(page, expectedButton) {
+ const sent = new URL(page.url()).searchParams;
+ expect(sent.get('email')).toBe('[email protected]');
+ for (const name of ['action', 'submit', 'pic.x', 'pic.y']) {
+ const values = sent.getAll(name);
+ const expected = expectedButton[name] || [];
+ expect(values, name).toHaveLength(expected.length);
+ expected.forEach((value, index) => value instanceof RegExp ?
expect(values[index]).toMatch(value) : expect(values[index]).toBe(value));
+ }
+}
+
+// Unomi does not answer the events until the test calls answer(): pending
holds the waiting requests
+async function holdEvents(page) {
+ const pending = [];
+ let answer;
+ const answered = new Promise(resolve => {
+ answer = resolve;
+ });
+ await page.route(UNOMI_URL + '/eventcollector', async route => {
+ pending.push(route);
+ await answered;
+ await route.continue();
+ });
+ return { pending, answer: () => answer() };
+}
+
+// The count found by findEvents stays the same 2s later: no duplicate event
arrives afterwards
+async function expectNoMoreEvents(targetId, count, options = {}) {
+ await new Promise(resolve => setTimeout(resolve, 2000));
+ expect(await events(targetId, Object.assign({}, options, { minCount: count
+ 1, timeoutMs: 1000 }))).toHaveLength(count);
+}
+
async function sendClick(page, label) {
- const targetId = label + '-' + Date.now() + '-' + Math.floor(Math.random()
* 1e6);
+ const targetId = uniqueId(label);
const result = await page.evaluate(id => window.trackerTest.sendClick(id),
targetId);
return Object.assign({ targetId }, result);
}
async function expectStoredClick(click) {
expect(click.sent).toBe(true);
- const events = await findClickEvents(process.env.UNOMI_READ_CREDENTIALS,
click.targetId);
- expect(events).toHaveLength(1);
+ const clickEvents = await events(click.targetId);
+ expect(clickEvents).toHaveLength(1);
}
test.describe(server.name, () => {
- test('the browser build runs without a Buffer polyfill', async ({ page })
=> {
+ test('the browser build runs without a Buffer polyfill (UNOMI-1023)',
async ({ page }) => {
const errors = [];
page.on('pageerror', error => errors.push(error.message));
const { context } = await openPage(page, LISTED_ORIGIN, siteConfig);
@@ -62,20 +112,19 @@ test.describe(server.name, () => {
expect(context.loaded).toBe(true);
});
- test('collects events from a page with empty "attributes", as the Unomi
manual showed', async ({ page }) => {
+ test('collects events from a page with empty "attributes", as the Unomi
manual showed (UNOMI-1024)', async ({ page }) => {
const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ pageAttributes: '{}' }, siteConfig));
expect(context.loaded).toBe(true);
await expectStoredClick(await sendClick(page,
'empty-page-attributes'));
});
- test('sends non-empty page "attributes" as before', async ({ page }) => {
+ test('sends non-empty page "attributes" as before (UNOMI-1024)', async ({
page }) => {
// No default page schema accepts attributes with content, so the
server drops the event, as with
// 1.5.0 (compare-1.5.0 checks that the request is the same). Unomi
2.0 to 2.3 accept an empty one.
await openPage(page, LISTED_ORIGIN, Object.assign({ pageAttributes:
'{"color":"blue"}' }, siteConfig));
const click = await sendClick(page, 'page-attributes');
expect(click.sent).toBe(true);
- const events = await
findClickEvents(process.env.UNOMI_READ_CREDENTIALS, click.targetId, 5000);
- expect(events).toHaveLength(0);
+ expect(await events(click.targetId, { timeoutMs: 5000
})).toHaveLength(0);
});
test('loads the context and collects an event with the site
configuration', async ({ page }) => {
@@ -101,4 +150,374 @@ test.describe(server.name, () => {
expect(context.loaded).toBe(true);
});
+
+ test('works when the configuration has no timeoutInMilliseconds
(UNOMI-1043)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ timeout: 'none' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await expectStoredClick(await sendClick(page, 'no-timeout'));
+ });
+
+ test('shows the personalized variant that Unomi chooses', async ({ page })
=> {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ personalization: 'true' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ expect(await page.evaluate(() =>
window.trackerTest.shownVariants())).toEqual(['variant-always']);
+ });
+
+ test('shows the fallback variant when Unomi cannot be reached', async ({
page }) => {
+ await page.route(UNOMI_URL + '/**', route =>
route.abort('connectionrefused'));
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ personalization: 'true' }, siteConfig));
+ expect(context.loaded).toBe(false);
+ expect(await page.evaluate(() =>
window.trackerTest.shownVariants())).toEqual(['variant-fallback']);
+ });
+
+ test('shows the fallback variant when Unomi does not answer in time',
async ({ page }) => {
+ // The requests are never answered: the tracker gives up after
timeoutInMilliseconds (1s
+ // here, instead of the page's 10s), so the fallback must come well
before 10s
+ await page.route(UNOMI_URL + '/**', () => {});
+ const start = Date.now();
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ personalization: 'true', timeout: '1000' }, siteConfig));
+ expect(Date.now() - start).toBeLessThan(5000);
+ expect(context.loaded).toBe(false);
+ expect(await page.evaluate(() =>
window.trackerTest.shownVariants())).toEqual(['variant-fallback']);
+ });
+
+ // Clicks on a button do not leave the page; clicks on a link to #arrived
do, as far as the tracker
+ // can tell (it holds them back), but keep the page open, so that the test
can go on
+ const CLICKED = [
+ { name: 'a button', config: {} },
+ { name: 'a link', config: { clickLink: 'hash' } }
+ ];
+ const SENT = [
+ { name: 'as an event listener', config: {} },
+ { name: 'with callbacks', config: { clickCallback: 'true' } }
+ ];
+
+ for (const clicked of CLICKED) {
+ for (const sent of SENT) {
+ test('tracks every click on ' + clicked.name + ' sent with
sendClickEvent ' + sent.name + ' (UNOMI-1037)', async ({ page }) => {
+ const elementId = uniqueId('tracked-element');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId }, clicked.config, sent.config,
siteConfig));
+ expect(context.loaded).toBe(true);
+ for (const count of [1, 2, 3]) {
+ await page.click('#' + elementId);
+ expect(await events(elementId, { minCount: count
})).toHaveLength(count);
+ }
+ await expectNoMoreEvents(elementId, 3);
+ if (sent.config.clickCallback) {
+ expect(await page.evaluate(() =>
window.trackerTest.clickCallbacks())).toEqual({ success: 3, error: 0 });
+ } else if (clicked.config.clickLink) {
+ // The click dispatched again follows the link
+ expect(new URL(page.url()).hash).toBe('#arrived');
+ }
+ });
+ }
+ }
+
+ test('runs the site\'s own click handler once per click on a button sent
with sendClickEvent (UNOMI-1046)', async ({ page }) => {
+ const elementId = uniqueId('tracked-button');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId }, siteConfig));
+ expect(context.loaded).toBe(true);
+ for (const count of [1, 2]) {
+ await page.click('#' + elementId);
+ expect(await events(elementId, { minCount: count
})).toHaveLength(count);
+ expect(await page.evaluate(() =>
window.trackerTest.siteClicks())).toBe(count);
+ }
+ });
+
+ test('lets a single-page application router handle a tracked link, without
reloading the page (UNOMI-1046)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true', router: 'true' },
siteConfig));
+ expect(context.loaded).toBe(true);
+ const pageLoadId = await page.evaluate(() =>
window.trackerTest.pageLoadId());
+ for (const count of [1, 2]) {
+ await page.click('#' + elementId);
+ expect(await events(elementId, { minCount: count
})).toHaveLength(count);
+ await expect.poll(() => page.evaluate(() =>
window.trackerTest.routed())).toBe(count);
+ }
+ expect(new URL(page.url()).pathname).toBe('/arrived');
+ expect(await page.evaluate(() =>
window.trackerTest.pageLoadId())).toBe(pageLoadId);
+ });
+
+ for (const sent of SENT) {
+ test('tracks a second click made before the first event is sent, ' +
sent.name + ' (UNOMI-1037)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'hash' }, sent.config,
siteConfig));
+ expect(context.loaded).toBe(true);
+ // Unomi does not answer until both clicks are made
+ const held = await holdEvents(page);
+ await page.click('#' + elementId);
+ await page.click('#' + elementId);
+ await expect.poll(() => held.pending.length).toBe(2);
+ held.answer();
+ expect(await events(elementId, { minCount: 2 })).toHaveLength(2);
+ await expectNoMoreEvents(elementId, 2);
+ if (sent.config.clickCallback) {
+ expect(await page.evaluate(() =>
window.trackerTest.clickCallbacks())).toEqual({ success: 2, error: 0 });
+ }
+ });
+ }
+
+ for (const clicked of CLICKED) {
+ test('calls the error callback on each click on ' + clicked.name + '
when the event cannot be sent (UNOMI-1037)', async ({ page }) => {
+ const elementId = uniqueId('tracked-element');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickCallback: 'true' },
clicked.config, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.route(UNOMI_URL + '/eventcollector', route =>
route.abort('connectionrefused'));
+ for (const count of [1, 2, 3]) {
+ await page.click('#' + elementId);
+ await expect.poll(() => page.evaluate(() =>
window.trackerTest.clickCallbacks().error)).toBe(count);
+ }
+ expect(await page.evaluate(() =>
window.trackerTest.clickCallbacks())).toEqual({ success: 0, error: 3 });
+ });
+ }
+
+ test('follows a tracked link when its event cannot be sent (UNOMI-1037)',
async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.route(UNOMI_URL + '/eventcollector', route =>
route.abort('connectionrefused'));
+ await page.click('#' + elementId);
+ await page.waitForURL('**/arrived', { timeout: 5000 });
+ });
+
+ test('follows a tracked link at once when Unomi could not be reached
(UNOMI-1040)', async ({ page }) => {
+ await page.route(UNOMI_URL + '/**', route =>
route.abort('connectionrefused'));
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true' }, siteConfig));
+ expect(context.loaded).toBe(false);
+ await page.click('#' + elementId);
+ await page.waitForURL('**/arrived', { timeout: 3000 });
+ });
+
+ test('follows a tracked link at once when tracking is turned off
(UNOMI-1040)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context, requests } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.evaluate(() => window.trackerTest.disableTracking());
+ const sentBefore = requests.length;
+ await page.click('#' + elementId);
+ await page.waitForURL('**/arrived', { timeout: 3000 });
+ expect(requests.slice(sentBefore).map(request =>
request.url())).toEqual([]);
+ });
+
+ test('opens a tracked link in a new tab with Ctrl or Cmd, and still tracks
the click (UNOMI-1041)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ const newTab = page.context().waitForEvent('page');
+ await page.click('#' + elementId, { modifiers: ['ControlOrMeta'] });
+ await newTab;
+ expect(new URL(page.url()).pathname).toBe('/');
+ expect(await events(elementId)).toHaveLength(1);
+ await expectNoMoreEvents(elementId, 1);
+ });
+
+ for (const child of [{ name: 'without', config: { clickChild: 'plain' } },
{ name: 'with', config: { clickChild: 'withId' } }]) {
+ test('tracks a click on a child, ' + child.name + ' its own id, of a
tracked link, and follows the link (UNOMI-1047)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true' }, child.config,
siteConfig));
+ expect(context.loaded).toBe(true);
+ // Until Unomi answers, the link is not followed
+ const held = await holdEvents(page);
+ await page.click('#' + elementId + ' .tracked-child');
+ await expect.poll(() => held.pending.length).toBe(1);
+ await page.waitForTimeout(500);
+ expect(new URL(page.url()).pathname).toBe('/');
+ held.answer();
+ await page.waitForURL('**/arrived', { timeout: 5000 });
+ expect(await events(elementId)).toHaveLength(1);
+ if (child.config.clickChild === 'withId') {
+ expect(await events(elementId + '-child', { timeoutMs: 2000
})).toHaveLength(0);
+ }
+ });
+
+ test('tracks a click on a child, ' + child.name + ' its own id, of a
tracked button, once (UNOMI-1047)', async ({ page }) => {
+ const elementId = uniqueId('tracked-button');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId }, child.config, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.click('#' + elementId + ' .tracked-child');
+ expect(await events(elementId)).toHaveLength(1);
+ await expectNoMoreEvents(elementId, 1);
+ expect(await page.evaluate(() =>
window.trackerTest.siteClicks())).toBe(1);
+ });
+ }
+
+ test('lets a router on the link itself handle a tracked link, once per
click (UNOMI-1040, UNOMI-1046)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'true', router: 'link' },
siteConfig));
+ expect(context.loaded).toBe(true);
+ const pageLoadId = await page.evaluate(() =>
window.trackerTest.pageLoadId());
+ for (const count of [1, 2]) {
+ await page.click('#' + elementId);
+ expect(await events(elementId, { minCount: count
})).toHaveLength(count);
+ await page.waitForTimeout(500);
+ expect(await page.evaluate(() =>
window.trackerTest.routed())).toBe(count);
+ }
+ expect(await page.evaluate(() =>
window.trackerTest.pageLoadId())).toBe(pageLoadId);
+ });
+
+ test('opens a tracked link to a new tab at once, without waiting for Unomi
(UNOMI-1040)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'blank' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ const held = await holdEvents(page);
+ const newTab = page.context().waitForEvent('page', { timeout: 3000 });
+ await page.click('#' + elementId);
+ await newTab;
+ expect(held.pending.length).toBe(1);
+ held.answer();
+ expect(await events(elementId)).toHaveLength(1);
+ });
+
+ test('downloads from a tracked link at once, without waiting for Unomi
(UNOMI-1040)', async ({ page }) => {
+ const elementId = uniqueId('tracked-link');
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ clickButton: elementId, clickLink: 'download' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.route(LISTED_ORIGIN + '/arrived', route => route.fulfill({
status: 200, contentType: 'text/plain', body: 'arrived' }));
+ const held = await holdEvents(page);
+ const download = page.waitForEvent('download', { timeout: 3000 });
+ await page.click('#' + elementId);
+ await download;
+ held.answer();
+ expect(await events(elementId)).toHaveLength(1);
+ });
+
+ for (const form of [{ name: 'a link', config: { clickButton: 'L',
clickLink: 'true' } }, { name: 'a form', config: { form: FORM_NAME, formSubmit:
'native' } }]) {
+ test('lets ' + form.name + ' go on at once when its event cannot even
be sent (UNOMI-1040)', async ({ page }) => {
+ const config = Object.assign({}, form.config);
+ if (config.clickButton) {
+ config.clickButton = uniqueId('tracked-link');
+ }
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign(config, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.evaluate(() => window.trackerTest.breakServerUrl());
+ await page.click(config.clickButton ? '#' + config.clickButton :
'#submit-form');
+ await page.waitForURL(config.clickButton ? '**/arrived' :
'**/submitted?**', { timeout: 3000 });
+ });
+
+ test('lets ' + form.name + ' go on after 1.5s at most when Unomi never
answers and there is no timeout (UNOMI-1040)', async ({ page }) => {
+ const config = Object.assign({ timeout: '-1' }, form.config);
+ if (config.clickButton) {
+ config.clickButton = uniqueId('tracked-link');
+ }
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign(config, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.route(UNOMI_URL + '/eventcollector', () => {});
+ const start = Date.now();
+ await page.click(config.clickButton ? '#' + config.clickButton :
'#submit-form');
+ await page.waitForURL(config.clickButton ? '**/arrived' :
'**/submitted?**', { timeout: 5000 });
+ expect(Date.now() - start).toBeGreaterThanOrEqual(1400);
+ });
+ }
+
+ test('sends the submit of a form that Unomi tracks, then lets the site
handle it (UNOMI-1042)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME }, siteConfig));
+ expect(context.loaded).toBe(true);
+ expect(await page.evaluate(() =>
window.trackerTest.watchedForms())).toContain(FORM_NAME);
+ // Until Unomi answers, the site does not see the submit
+ const held = await holdEvents(page);
+ await page.click('#submit-form');
+ await expect.poll(() => held.pending.length).toBe(1);
+ await page.waitForTimeout(500);
+ expect(await page.evaluate(() =>
window.trackerTest.siteSubmits())).toEqual([]);
+ held.answer();
+ const formEvents = await events(FORM_NAME, { matching:
thisSession(context) });
+ expect(formEvents).toHaveLength(1);
+ expect(formEvents[0].eventType).toBe('form');
+ // Each kind of field: the checked checkboxes and the selected options
as lists, the checked
+ // radio button, the text area; the empty field is left out
+ expect(formEvents[0].flattenedProperties.fields).toEqual({
+ email: '[email protected]',
+ topics: ['news'],
+ country: 'ch',
+ languages: ['fr', 'de'],
+ contact: 'email',
+ message: 'Hello'
+ });
+ // Then the site gets the submit once (not also the visitor's own),
which says which button was used
+ await expect.poll(() => page.evaluate(() =>
window.trackerTest.siteSubmits().length)).toBe(1);
+ await page.waitForTimeout(1000);
+ expect(await page.evaluate(() =>
window.trackerTest.siteSubmits())).toEqual([{ submitter: 'submit-form' }]);
+ await expectNoMoreEvents(FORM_NAME, 1, { matching:
thisSession(context) });
+ });
+
+ // Browsers with HTMLFormElement.requestSubmit, and older ones, where the
tracker submits the form itself
+ const BROWSERS = [
+ { name: '', config: {} },
+ { name: ', in a browser without requestSubmit', config: { oldBrowser:
'true' } }
+ ];
+ const BUTTONS = [
+ { name: 'the button that was used', id: 'delete-form', expected: {
action: ['delete'] } },
+ { name: 'a button named "submit"', id: 'submit-named', expected: {
submit: ['go'] } },
+ { name: 'an image button', id: 'submit-image', expected: { 'pic.x':
[/^\d+$/], 'pic.y': [/^\d+$/] } },
+ { name: 'a button the site removes meanwhile', id: 'submit-removed',
expected: { action: ['removed'] } }
+ ];
+
+ for (const browser of BROWSERS) {
+ for (const button of BUTTONS) {
+ test('submits a tracked form with ' + button.name + ', as a native
submit does' + browser.name + ' (UNOMI-1042)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native' }, browser.config,
siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.click('#' + button.id);
+ await page.waitForURL('**/submitted?**', { timeout: 5000 });
+ expectSubmitted(page, button.expected);
+ expect(await events(FORM_NAME, { matching:
thisSession(context) })).toHaveLength(1);
+ await expectNoMoreEvents(FORM_NAME, 1, { matching:
thisSession(context) });
+ });
+ }
+
+ test('submits a tracked form with its button when the event cannot be
sent' + browser.name + ' (UNOMI-1042)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native' }, browser.config,
siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.route(UNOMI_URL + '/eventcollector', route =>
route.abort('connectionrefused'));
+ await page.click('#delete-form');
+ await page.waitForURL('**/submitted?**', { timeout: 5000 });
+ expectSubmitted(page, { action: ['delete'] });
+ });
+ }
+
+ test('submits a tracked form into a new tab at once, without waiting for
Unomi (UNOMI-1040)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native', formTarget: '_blank' },
siteConfig));
+ expect(context.loaded).toBe(true);
+ const held = await holdEvents(page);
+ const newTab = page.context().waitForEvent('page', { timeout: 3000 });
+ await page.click('#delete-form');
+ const tab = await newTab;
+ await tab.waitForURL('**/submitted?**', { timeout: 3000 });
+ expect(new
URL(tab.url()).searchParams.getAll('action')).toEqual(['delete']);
+ held.answer();
+ expect(await events(FORM_NAME, { matching: thisSession(context)
})).toHaveLength(1);
+ });
+
+ test('submits a tracked form with Enter, as a native submit does
(UNOMI-1042)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.focus('input[name="email"]');
+ await page.keyboard.press('Enter');
+ await page.waitForURL('**/submitted?**', { timeout: 5000 });
+ // Enter submits with the form's first submit button
+ expectSubmitted(page, { action: ['send'] });
+ expect(await events(FORM_NAME, { matching: thisSession(context)
})).toHaveLength(1);
+ });
+
+ test('tracks a click on a submit button of a tracked form, and its submit
(UNOMI-1037, UNOMI-1042)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native', trackSubmitButton:
'true' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.click('#delete-form');
+ await page.waitForURL('**/submitted?**', { timeout: 5000 });
+ expectSubmitted(page, { action: ['delete'] });
+ expect(await events('delete-form', { matching: thisSession(context)
})).toHaveLength(1);
+ expect(await events(FORM_NAME, { matching: thisSession(context)
})).toHaveLength(1);
+ await expectNoMoreEvents('delete-form', 1, { matching:
thisSession(context) });
+ await expectNoMoreEvents(FORM_NAME, 1, { matching:
thisSession(context) });
+ });
+
+ test('submits a tracked form at once when tracking is turned off
(UNOMI-1040)', async ({ page }) => {
+ const { context } = await openPage(page, LISTED_ORIGIN,
Object.assign({ form: FORM_NAME, formSubmit: 'native' }, siteConfig));
+ expect(context.loaded).toBe(true);
+ await page.evaluate(() => window.trackerTest.disableTracking());
+ await page.click('#submit-form');
+ await page.waitForURL('**/submitted?**', { timeout: 3000 });
+ expectSubmitted(page, { action: ['send'] });
+ });
});
diff --git a/test/servers/unomi.js b/test/servers/unomi.js
index bea6a2b..83c3fb6 100644
--- a/test/servers/unomi.js
+++ b/test/servers/unomi.js
@@ -100,6 +100,48 @@ async function retry(action, timeoutMs = 120000) {
}
}
+// The endpoints the tests use answer. A starting server registers its
endpoints one by one, and can
+// remove and add them again while it starts, so a single answer is not
enough. The event collector
+// refuses an empty list of events (400), which shows it is there without
storing anything.
+async function endpointsAnswer(credentials) {
+ const probe = { sessionId: 'tracker-test-probe', events: [] };
+ const post = (path, body, headers) => fetch(UNOMI_URL + path, {
+ method: 'POST',
+ headers: Object.assign({ 'Content-Type': 'application/json' },
headers),
+ body: JSON.stringify(body)
+ });
+ const context = await post('/cxs/context.json', probe);
+ const collector = await post('/cxs/eventcollector', probe);
+ const search = await post('/cxs/events/search', { limit: 1 }, {
Authorization: basic(credentials) });
+ return context.status === 200 && (collector.status === 200 ||
collector.status === 400) && search.status === 200;
+}
+
+// Waits until the server has kept answering for a while: STABLE_ROUNDS checks
in a row, 2s apart.
+const STABLE_ROUNDS = 5;
+async function waitUntilStable(credentials, timeoutMs = 180000) {
+ const deadline = Date.now() + timeoutMs;
+ let rounds = 0;
+ let lastProblem = 'no check yet';
+ while (rounds < STABLE_ROUNDS) {
+ let ok = false;
+ try {
+ if (await endpointsAnswer(credentials)) {
+ ok = true;
+ } else {
+ lastProblem = 'an endpoint did not answer as expected';
+ }
+ } catch (e) {
+ // not answering yet
+ lastProblem = String(e);
+ }
+ rounds = ok ? rounds + 1 : 0;
+ if (Date.now() > deadline) {
+ throw new Error('Unomi did not answer steadily at ' + UNOMI_URL +
' within ' + timeoutMs / 1000 + 's (last problem: ' + lastProblem + ')');
+ }
+ await sleep(2000);
+ }
+}
+
// Creates the scope if it is missing, and waits until the server returns it.
async function ensureScope(credentials) {
await retry(async () => {
@@ -118,34 +160,65 @@ async function ensureScope(credentials) {
});
}
-// Prepares the server before the tests: the scope that the tracker events use.
-// Returns the credentials that can read the events.
+// A rule on the submits of the test form. Unomi returns its condition to the
tracker in the
+// "tracked conditions" of the context, which is how the tracker knows which
forms to watch.
+const FORM_NAME = 'tracker-test-form';
+async function ensureFormRule(credentials) {
+ await request('POST', '/cxs/rules', {
+ credentials: credentials,
+ body: {
+ metadata: { id: 'tracker-test-form-rule', name: 'Tracker test
form', scope: SCOPE, enabled: true },
+ condition: { type: 'formEventCondition', parameterValues: {
formId: FORM_NAME } },
+ actions: [{ type: 'setPropertyAction', parameterValues: {
setPropertyName: 'properties.trackerTestForm', setPropertyValue: 'sent',
setPropertyStrategy: 'alwaysSet' } }]
+ }
+ });
+ // Rules are loaded in the background: wait until the context returns the
condition
+ await retry(async () => {
+ const context = await request('POST', '/cxs/context.json', {
+ body: { sessionId: 'tracker-test-probe', source: { scope: SCOPE,
itemType: 'page', itemId: 'tracker-test-page' } }
+ });
+ const tracked = (context.trackedConditions || []).some(condition =>
condition.parameterValues && condition.parameterValues.formId === FORM_NAME);
+ if (!tracked) {
+ throw new Error('The form rule is not in the tracked conditions
yet');
+ }
+ });
+}
+
+// Prepares the server before the tests: the scope that the tracker events
use, and the rule that
+// makes the tracker watch the test form. Returns the credentials that can
read the events.
async function setUp(t) {
await waitForServer();
await ensureScope(t.admin);
+ await waitUntilStable(t.admin);
+ await ensureFormRule(t.admin);
console.log('Server ready: ' + t.name + ', scope ' + SCOPE);
return { credentials: t.admin };
}
-// Polls until click events on the given target are indexed, and returns them.
-async function findClickEvents(credentials, targetId, timeoutMs = 15000) {
+// Polls until at least minCount events on the given target are indexed, and
returns them, newest
+// first. Only the 10 newest events on the target are read; with "matching",
only those it accepts
+// count and are returned (a server kept from an earlier run can hold events
on the same target).
+// After timeoutMs it returns what it found, which can be fewer than minCount.
+async function findEvents(credentials, targetId, { timeoutMs = 15000, minCount
= 1, matching = () => true } = {}) {
const deadline = Date.now() + timeoutMs;
for (;;) {
const result = await request('POST', '/cxs/events/search', {
credentials: credentials,
body: {
limit: 10,
+ sortby: 'timeStamp:desc',
condition: {
type: 'eventPropertyCondition',
parameterValues: { propertyName: 'target.itemId',
comparisonOperator: 'equals', propertyValue: targetId }
}
}
});
- if ((result.list && result.list.length > 0) || Date.now() > deadline) {
- return result.list || [];
+ const events = (result.list || []).filter(matching);
+ if (events.length >= minCount || Date.now() > deadline) {
+ return events;
}
await sleep(1000);
}
}
-module.exports = { UNOMI_URL, TARGETS, target, setUp, findClickEvents };
+module.exports = { UNOMI_URL, TARGETS, FORM_NAME, target, setUp, findEvents };
diff --git a/test/spec.js b/test/spec.js
index d28dd4b..3037d34 100644
--- a/test/spec.js
+++ b/test/spec.js
@@ -23,7 +23,7 @@ assert(tracker !== null);
globalThis.document = { cookie: 'wem-session-id=test-session' };
-// Crawler detection: the same result with and without Node's Buffer
+// UNOMI-1023: crawler detection, the same result with and without Node's
Buffer
const crawlerTracker = unomi.useTracker();
const edgeStrings = ['', 'abc', 'é漢字😀', '\uD800', 'a\uDC00b', '😀\uD800',
'x'.repeat(4096), 'é'.repeat(2048), 'é'.repeat(2049)];
for (let i = 0; i < 500; i++) {
@@ -57,7 +57,7 @@ try {
globalThis.Buffer = nodeBuffer;
}
-// Page properties sent with events: an empty "attributes" is left out,
digitalData.page is unchanged
+// UNOMI-1024: page properties sent with events: an empty "attributes" is left
out, digitalData.page is unchanged
function pagePropertiesFor(attributes) {
const page = { pageInfo: { pageID: 'home', pageName: 'Home' },
consentTypes: [] };
if (attributes !== undefined) {
@@ -82,4 +82,251 @@ assert.deepStrictEqual(Object.keys(pageResult.target),
['pageInfo', 'consentType
assert.strictEqual(pageResult.source.pageInfo, pageResult.page.pageInfo,
'pageInfo is the same object');
assert.deepStrictEqual(pageResult.page.attributes, {}, 'digitalData.page keeps
its attributes');
-console.log('Tests passed');
\ No newline at end of file
+// UNOMI-1043: without a usable timeoutInMilliseconds, the tracker uses its
default instead of a timer
+// that fires at once
+function timeoutFor(timeoutInMilliseconds) {
+ const t = unomi.useTracker();
+ const wemInitConfig = { contextServerUrl: '' };
+ if (timeoutInMilliseconds !== 'missing') {
+ wemInitConfig.timeoutInMilliseconds = timeoutInMilliseconds;
+ }
+ t.initTracker({ wemInitConfig });
+ return t.timeoutInMilliseconds;
+}
+assert.strictEqual(timeoutFor('missing'), 1500, 'no timeout: the default');
+for (const value of [undefined, null, '', ' ', 'soon', NaN, Infinity, 0, -5,
'0']) {
+ assert.strictEqual(timeoutFor(value), 1500, 'the default for ' +
JSON.stringify(String(value)));
+}
+assert.strictEqual(timeoutFor(3000), 3000, 'a configured timeout is kept');
+assert.strictEqual(timeoutFor(-1), -1, '-1 (no timeout) is kept');
+assert.strictEqual(timeoutFor('3000'), 3000, 'a number given as a string, as
from an HTML attribute, is used');
+assert.strictEqual(timeoutFor('-1'), -1, '-1 given as a string is kept');
+
+// UNOMI-1037, UNOMI-1040, UNOMI-1041, UNOMI-1046, UNOMI-1047: sendClickEvent,
what happens to the
+// visitor's click. Fake elements have attributes and a parent; click() runs
the tracker's listener on
+// the tracked element as a browser does (currentTarget is the tracked
element, target the clicked
+// one). collectEvent is replaced, so each test sends or fails the event
itself.
+function fakeElement(localName, attributes = {}, properties = {}) {
+ const element = Object.assign({
+ localName,
+ parentElement: null,
+ getAttribute: name => Object.prototype.hasOwnProperty.call(attributes,
name) ? attributes[name] : null,
+ hasAttribute: name => Object.prototype.hasOwnProperty.call(attributes,
name)
+ }, properties);
+ if (attributes.id) {
+ element.id = attributes.id;
+ }
+ return element;
+}
+
+const FORM = fakeElement('form');
+const ELEMENTS = {
+ // Clicks that leave the page
+ link: () => fakeElement('a', { href: '/next' }),
+ linkToTop: () => fakeElement('a', { href: '/next', target: '_top' }),
+ area: () => fakeElement('area', { href: '/next' }),
+ submitButton: () => fakeElement('button', {}, { type: 'submit', form: FORM
}),
+ submitInput: () => fakeElement('input', {}, { type: 'submit', form: FORM
}),
+ imageInput: () => fakeElement('input', {}, { type: 'image', form: FORM }),
+ // Clicks that do not
+ linkWithoutAddress: () => fakeElement('a'),
+ linkToFragment: () => fakeElement('a', { href: '#part' }),
+ linkToScript: () => fakeElement('a', { href: 'javascript:void(0)' }),
+ linkToNewTab: () => fakeElement('a', { href: '/next', target: '_blank' }),
+ download: () => fakeElement('a', { href: '/file.pdf', download: '' }),
+ submitButtonToNewTab: () => fakeElement('button', { formtarget: '_blank'
}, { type: 'submit', form: FORM }),
+ button: () => fakeElement('button', {}, { type: 'button', form: FORM }),
+ submitButtonWithoutForm: () => fakeElement('button', {}, { type: 'submit',
form: null }),
+ checkbox: () => fakeElement('input', {}, { type: 'checkbox', form: FORM }),
+ div: () => fakeElement('div')
+};
+const LEAVING = ['link', 'linkToTop', 'area', 'submitButton', 'submitInput',
'imageInput'];
+
+function clickTracker(kind = 'link', { fallback = false, timeout = 10000 } =
{}) {
+ const t = unomi.useTracker();
+ t.initTracker({ scope: 'test', site: { siteInfo: { siteID: 'test' } },
page: { pageInfo: { pageID: 'home', pageName: 'Home' }, consentTypes: [] },
events: [], wemInitConfig: { contextServerUrl: '', timeoutInMilliseconds:
timeout } });
+ t.fallback = fallback;
+ const sent = [];
+ t.collectEvent = (event, success, error) => sent.push({ event, success,
error });
+ const element = ELEMENTS[kind]();
+ element.id = 'element';
+ element.dispatchedAgain = [];
+ element.click = () => {
+ const event = clickOn(element);
+ t.sendClickEvent(event);
+ element.dispatchedAgain.push(event);
+ };
+ return { t, sent, element };
+}
+
+// A click on clicked, seen by the tracker's listener on listening (the
clicked element by default)
+function clickOn(clicked, keys = {}, listening = clicked) {
+ const event = {
+ target: clicked,
+ currentTarget: listening,
+ defaultPrevented: false,
+ preventDefault() {
+ event.defaultPrevented = true;
+ }
+ };
+ return Object.assign(event, keys);
+}
+
+let clicks;
+let click;
+for (const kind of Object.keys(ELEMENTS)) {
+ const leaves = LEAVING.includes(kind);
+ for (const outcome of ['success', 'error']) {
+ clicks = clickTracker(kind);
+ click = clickOn(clicks.element);
+ clicks.t.sendClickEvent(click);
+ assert.strictEqual(click.defaultPrevented, leaves, kind + ': the click
is ' + (leaves ? '' : 'not ') + 'held back while its event is sent');
+ assert.strictEqual(clicks.sent.length, 1, kind + ': one event is
sent');
+ assert.strictEqual(clicks.sent[0].event.target.itemId, 'element', kind
+ ': the event is on the element');
+ clicks.sent[0][outcome]({});
+ assert.strictEqual(clicks.element.dispatchedAgain.length, leaves ? 1 :
0, kind + ', ' + outcome + ': the click is ' + (leaves ? '' : 'not ') +
'dispatched again');
+ if (leaves) {
+
assert.strictEqual(clicks.element.dispatchedAgain[0].defaultPrevented, false,
kind + ': the click dispatched again goes through');
+ }
+ assert.strictEqual(clicks.sent.length, 1, kind + ': the click
dispatched again is not sent');
+ assert.deepStrictEqual(clicks.t.eventsPrevented, [], kind + ': nothing
is left marked');
+ clicks.sent[0][outcome === 'success' ? 'error' : 'success']({});
+ assert.strictEqual(clicks.element.dispatchedAgain.length, leaves ? 1 :
0, kind + ': a second outcome for the same click does nothing');
+ clicks.t.sendClickEvent(clickOn(clicks.element));
+ assert.strictEqual(clicks.sent.length, 2, kind + ': the next click is
sent too');
+ }
+}
+
+// An image, an icon or some text inside the tracked link or button
(UNOMI-1047)
+for (const childId of [undefined, 'icon']) {
+ for (const kind of ['link', 'submitButton', 'button']) {
+ clicks = clickTracker(kind);
+ const child = fakeElement('img', childId ? { id: childId } : {});
+ child.parentElement = clicks.element;
+ child.dispatchedAgain = 0;
+ child.click = () => {
+ child.dispatchedAgain++;
+ clicks.t.sendClickEvent(clickOn(child, {}, clicks.element));
+ };
+ click = clickOn(child, {}, clicks.element);
+ clicks.t.sendClickEvent(click);
+ const leaves = kind !== 'button';
+ const label = kind + ' with a child ' + (childId ? 'with' : 'without')
+ ' an id';
+ assert.strictEqual(clicks.sent.length, 1, label + ': the click is
sent');
+ assert.strictEqual(clicks.sent[0].event.target.itemId, 'element',
label + ': on the tracked element');
+ assert.strictEqual(click.defaultPrevented, leaves, label + ': held
back as for the element itself');
+ clicks.sent[0].success({});
+ assert.strictEqual(child.dispatchedAgain, leaves ? 1 : 0, label + ':
dispatched again on the clicked child');
+ assert.strictEqual(clicks.sent.length, 1, label + ': the click
dispatched again is not sent');
+ }
+}
+
+// Called from a listener on the document: the clicked element is tracked, as
before
+clicks = clickTracker();
+click = clickOn(clicks.element, {}, { nodeType: 9 });
+clicks.t.sendClickEvent(click);
+assert.strictEqual(clicks.sent.length, 1, 'from a listener on the document,
the click is sent');
+assert.strictEqual(clicks.sent[0].event.target.itemId, 'element', 'on the
clicked element');
+assert.strictEqual(click.defaultPrevented, true, 'and held back');
+
+// A click the site already prevented (a router on the link, registered first)
is the site's
+clicks = clickTracker();
+click = clickOn(clicks.element, { defaultPrevented: true });
+clicks.t.sendClickEvent(click);
+assert.strictEqual(clicks.sent.length, 1, 'a click the site prevented is
sent');
+clicks.sent[0].success({});
+assert.strictEqual(clicks.element.dispatchedAgain.length, 0, 'and not
dispatched again');
+
+clicks = clickTracker();
+clicks.t.sendClickEvent(clickOn(clicks.element));
+clicks.t.sendClickEvent(clickOn(clicks.element));
+assert.strictEqual(clicks.sent.length, 2, 'a second click before the first
event is sent is sent too');
+clicks.sent[0].success({});
+clicks.sent[1].success({});
+assert.deepStrictEqual(clicks.element.dispatchedAgain.map(event =>
event.defaultPrevented), [false, false], 'each click is dispatched again once');
+
+for (const kind of ['link', 'button']) {
+ const callbacks = [];
+ clicks = clickTracker(kind);
+ for (const outcome of ['success', 'error', 'success']) {
+ click = clickOn(clicks.element);
+ clicks.t.sendClickEvent(click, () => callbacks.push('success'), () =>
callbacks.push('error'));
+ assert.strictEqual(click.defaultPrevented, kind === 'link', kind + '
with callbacks: held back only if the click leaves the page');
+ clicks.sent[clicks.sent.length - 1][outcome]({});
+ }
+ assert.strictEqual(clicks.sent.length, 3, kind + ' with callbacks: every
click is sent');
+ assert.deepStrictEqual(callbacks, ['success', 'error', 'success'], kind +
' with callbacks: the callback of each outcome is called');
+ assert.strictEqual(clicks.element.dispatchedAgain.length, 0, kind + ' with
callbacks: the site handles the click itself');
+}
+
+for (const kind of ['link', 'button']) {
+ clicks = clickTracker(kind, { fallback: true });
+ click = clickOn(clicks.element);
+ clicks.t.sendClickEvent(click);
+ assert.strictEqual(click.defaultPrevented, false, kind + ' in fallback:
the click goes through');
+ assert.strictEqual(clicks.sent.length, 0, kind + ' in fallback: nothing is
sent');
+}
+
+for (const key of ['metaKey', 'ctrlKey', 'shiftKey', 'altKey']) {
+ clicks = clickTracker();
+ click = clickOn(clicks.element, { [key]: true });
+ clicks.t.sendClickEvent(click);
+ assert.strictEqual(click.defaultPrevented, false, 'a click with ' + key +
' goes through (new tab or window)');
+ assert.strictEqual(clicks.sent.length, 1, 'a click with ' + key + ' is
sent');
+ clicks.sent[0].success({});
+ assert.strictEqual(clicks.element.dispatchedAgain.length, 0, 'and is not
dispatched again');
+ click = clickOn(clicks.element, { [key]: true });
+ clicks.t.sendClickEvent(click, () => {});
+ assert.strictEqual(click.defaultPrevented, true, 'with a callback, a click
with ' + key + ' is held back for the site');
+}
+
+// When sending the event throws at once, the click goes on, or the error
callback is called
+clicks = clickTracker();
+clicks.t.collectEvent = () => {
+ throw new Error('cannot send');
+};
+click = clickOn(clicks.element);
+clicks.t.sendClickEvent(click);
+assert.strictEqual(click.defaultPrevented, true, 'when sending throws, the
click was held back');
+assert.strictEqual(clicks.element.dispatchedAgain.length, 0, 'not dispatched
again during the visitor\'s click');
+let thrownErrors = 0;
+clicks.t.sendClickEvent(clickOn(clicks.element), () => {}, () =>
thrownErrors++);
+const throwing = clicks;
+setTimeout(() => {
+ assert.strictEqual(throwing.element.dispatchedAgain.length, 1, 'dispatched
again right after it');
+ assert.strictEqual(thrownErrors, 1, 'with callbacks, the error callback is
called');
+}, 0);
+
+clicks = clickTracker();
+click = clickOn(fakeElement('a', { href: '/next' }));
+clicks.t.sendClickEvent(click);
+assert.strictEqual(click.defaultPrevented, false, 'an element without id or
name is not tracked');
+assert.strictEqual(clicks.sent.length, 0, 'and nothing is sent');
+
+// Without a request timeout (-1), a held back click goes on after 1.5s at
most, once
+const noAnswer = clickTracker('link', { timeout: -1 });
+noAnswer.t.sendClickEvent(clickOn(noAnswer.element));
+assert.strictEqual(noAnswer.element.dispatchedAgain.length, 0, 'without a
timeout, the click is held back at first');
+setTimeout(() => {
+ assert.strictEqual(noAnswer.element.dispatchedAgain.length, 0, 'still held
back after 1s');
+}, 1000);
+setTimeout(() => {
+ assert.strictEqual(noAnswer.element.dispatchedAgain.length, 1, 'dispatched
again after 1.5s');
+ noAnswer.sent[0].success({});
+ assert.strictEqual(noAnswer.element.dispatchedAgain.length, 1, 'a late
answer from Unomi does not dispatch it again');
+ // The last check, which runs after all the others
+ console.log('Tests passed');
+}, 1700);
+
+// UNOMI-1038: ids of tracked videos and links, mapped through
digitalData.sourceLocalIdentifierMap
+function resolveId(id, sourceLocalIdentifierMap) {
+ const t = unomi.useTracker();
+ t.initTracker({ sourceLocalIdentifierMap: sourceLocalIdentifierMap,
wemInitConfig: { contextServerUrl: '', timeoutInMilliseconds: -1 } });
+ return t._resolveId(id);
+}
+
+const idMap = { 'live-': 'default-' };
+assert.strictEqual(resolveId('live-video', idMap), 'default-video', 'a mapped
part at the start of the id');
+assert.strictEqual(resolveId('page-live-video', idMap), 'page-default-video',
'a mapped part inside the id');
+assert.strictEqual(resolveId('other-video', idMap), 'other-video', 'an id
without a mapped part is unchanged');
+assert.strictEqual(resolveId('live-video', undefined), 'live-video', 'no map,
no change');