peeping-tom v0.0.6
peeping-tom
Event delegate for user journey and interaction tracking. Provides performant and configurable event capture via an easy to maintain object format.
See examples for demonstration.
Attribute
<button data-peeping-tom="signup"></button>data-peeping-tom: String
Unique key corresponding to a target object that should be defined using the properties described below.
Methods
watch()
The peepingTom.watch() method instantiates the library using provided parameters.
Syntax
peepingTom.watch(targets[, config]);Parameters
targets:
Objectkey:
StringObject key used to map functionality to element(s) containing a corresponding
data-peeping-tomHTML attribute. Supports regular expression anchors to target element collections using affixes^prefix-,-suffix$.value:
Objectevents:
String|String[]Event(s) to track:
'click','view'.data:
Interface|Function|peepingTom.DeferredInterfaceany data type to be provided as an argument to fn.Functiondefinition that is invoked upon event occurrence. Provided eventStringand nodeElementarguments.peepingTom.Deferredclass can be used to create an asynchronous, mutual dependency between event occurrence and invocation of theresolvemethod.
fn:
FunctionFunction definition to be invoked.
visible:
Number|OptionalFloating point number representing a specific visibility threshold at which to invoke the
'view'event. Expects positive value between0.0and1.0, where1.0equals entire element visibility.
config:
Object|Optionaldataset:
StringDataset name to override the default
data-peeping-tomHTML attribute. Each to their own!root:
ElementTop-level element on which to capture events. Defaults to
document.body.visible:
NumberGlobal element visibility threshold. Floating point number representing a specific visibility threshold at which to invoke the
'view'event. Expects positive value between0.0and1.0, where1.0equals entire element visibility. Defaults to0.8.
resolve()
Sets data property value asynchronously.
Requires: Asynchronous data declaration using — data: new peepingTom.Deferred.
Syntax
peepingTom.resolve(key, data);Parameters
key:
StringString matching a
targetsproperty key that was declared with aDeferreddata value.data:
Interface|FunctionAny data value that you wish to provide to the given
fn. If a value of typeFunctionis provided, it will be invoked and the returned value passed tofn.
disconnect()
Removes event listeners and observers.
Syntax
peepingTom.disconnect();Examples
targets: Object
const targets = {
'signup': {
events: 'click',
data: {
'event': 'signup-click',
'pageCategory': 'signup',
'visitorType': 'high-value',
},
fn: (data) => dataLayer.push(data),
},
'^recommended-product-': {
events: ['click', 'view'],
data: new peepingTom.Deferred,
fn: (data) => dataLayer.push(data),
visible: 0.6,
}
};watch: Function
peepingTom.watch(targets);resolve: Function
peepingTom.resolve('^recommended-product-', { type: 'previously-viewed' });Compatibility
- IntersectionObserver polyfill is required for the usual suspects. Suggestion: w3c/IntersectionObserver/polyfill.
Troubleshooting
- Click event capture: If click events are being blocked other elements. Try applying the CSS rule
pointer-events: none;to the overlapping elements. This will allow pointer events to pass through to the desired target.