Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 2x 2x 2x 2x 522x 174x 174x 174x 174x 174x 174x 174x 176x 2x 174x 2x 172x 172x 178x 1224x 1050x 174x 174x 174x 178x 174x 2x | /**
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/**
Signature function used to track the usage of Coral components. By default, there is no out of the box
implementation as tracking is agnostic of the underlying technology.
You need to implement a new tracker and add it with: <code>Coral.tracking.addListener(fn(){ });</code>
The <code>fn()</code> callback will receive multiple arguments:
* <code>trackData</code> - an object with fixed structure e.g. <code>{type: "button", eventType: "click", element: "save settings", feature: "sites"}</code>
* <code>event</code> - the CustomEvent or MouseEvent details object.
* <code>component</code> - the component reference object.
Using the above data you can map it to your own analytics tracker.
*/
class Tracking {
/* @ignore */
constructor() {
/**
All registered trackers.
@type {Array<Function>}
*/
this._trackers = [];
}
/**
Returns <code>true</code> if the tracking is disabled for the given component, otherwise false.
@param {HTMLElement} component
@returns {Boolean}
*/
_isTrackingDisabledForComponent(component) {
return component &&
typeof component.tracking !== 'undefined' &&
component.tracking === component.constructor.tracking.OFF;
}
/**
Get tracking annotations from the parent Component to which the event was bound.
@param {BaseComponent} component
@returns {{trackingElement: String, trackingElement: String}}
*/
_getTrackingDataFromComponentAttr(component) {
if (this._isTrackingDisabledForComponent(component)) {
return {
trackingElement: '',
trackingFeature: ''
};
}I
// Eg. from DOM trackingfeature="sites"
const trackingFeature = component.trackingFeature || '';
// Eg. from DOM trackingelement="rail toggle"
const trackingElement = component.trackingElement || '';
return {trackingFeature, trackingElement};
}
/**
Returns a tracking data object that can be used to compile data to send to an actual Analytics tracker.
@param {String} eventType
@param {String} targetType
@param {CustomEvent} event
@param {BaseComponent} component
@param {BaseComponent} childComponent
@returns {Object} An object with the tracking data.
*/
_createTrackingData(eventType, targetType, event, component, childComponent) {
const parentComponentType = (component.getAttribute('is') || component.tagName).toLowerCase();
// Gather data into the Coral Tracking structure.
const trackDataFromAttr = this._getTrackingDataFromComponentAttr(component);
// Compile data
/**
The default Coral tracking data object
filled with values from root Component and child Component (if exists).
@type {{targetType: string, targetElement: string, eventType: string, rootElement: string, rootFeature: string, rootType: string}}
*/
return {
targetType: targetType || parentComponentType || '',
targetElement: childComponent && childComponent.trackingElement ? childComponent.trackingElement : component.trackingElement,
eventType: eventType || event.type,
rootElement: trackDataFromAttr.trackingElement,
rootFeature: trackDataFromAttr.trackingFeature,
rootType: parentComponentType
};
}
/**
Add a tracking callback. This will be invoked every time a tracking event is emitted.
@param {TrackingCallback} trackingCallback
The callback to execute.
*/
addListener(trackingCallback) {
if (typeof trackingCallback !== 'function') {
throw new Error('Coral.Tracking: Tracker must be a function callback.');
}
if (this._trackers.indexOf(trackingCallback) !== -1) {
throw new Error('Coral.Tracking: Tracker callback cannot be added twice.');
}
this._trackers.push(trackingCallback);
}
/**
Removes a tracker.
@param {TrackingCallback} trackingCallback
*/
removeListener(trackingCallback) {
this._trackers = this._trackers.filter(trackerFn => trackerFn !== trackingCallback);
}
/**
Notify all trackers subscribed.
@param {String} eventType
Eg. click, select, etc.
@param {String} targetType
Eg. cycle button, cycle button item, etc.
@param {CustomEvent} event
@param {BaseComponent} component
@param {BaseComponent} childComponent
Optional, in case the event occurred on a child component.
@returns {Boolean} if the event was dispatch to at least 1 tracker.
*/
track(eventType, targetType, event, component, childComponent) {
if (
this._trackers.length === 0 ||
this._isTrackingDisabledForComponent(component) ||
this._isTrackingDisabledForComponent(childComponent)
) {
return false;
}
const args = Array.prototype.slice.call(arguments, [2]);
const trackingData = this._createTrackingData(eventType, targetType, event, component, childComponent);
this._trackers.forEach((trackerFn) => {
trackerFn.apply(null, [trackingData].concat(args));
});
return true;
}
}
/**
Executes the callback when ever there is an interaction inside the component that needs to be tracked. This can be used
to get insight on how users interact with the page and the features that available.
@typedef {function} TrackingCallback
@param {Object} trackData
Object containing the data to be tracked. It contains the properties <code>type</code>, <code>eventType</code>,
<code>element</code> and <code>feature</code>.
@param {CustomEvent} event
Underlying event that was generated by the user
@param {HTMLElement} component
Component that triggered the tracking event.
*/
/**
Tracking API to get insight on component usage.
@type {Tracking}
*/
const tracking = new Tracking();
export default tracking;
|