We'll introduce you to the UV API, what it's used for, and how to set it up and use it.
The Universal Variable API is a global event emitter used to pass underlying page or view information to third-party scripts. Using the UV API script, all data is exposed as events using window.uv
. All events must conform to schemas defined in the QProtocol schemas.
The UV API should be included as an inline script on the webpage at the top of head, immediately after the <meta charset... />
tag. By embedding the API in the head synchronously, any script in the page is able to emit or handle events without polling or waiting for asynchronous scripts to load.
<script>
!function(){function n(){function n(n){p.level=n}function e(n,e){p.info(n,"event emitted"),e=c(e||{}),e.meta=e.meta||{},e.meta.type=n,u.push(e),r(),v.listeners=f(v.listeners,function(n){return!n.disposed})}function o(n,e,o){function r(){return p.info("Replaying events"),t(function(){s(v.events,function(t){c.disposed||l(n,t.meta.type)&&e.call(o,t)})}),f}function i(){return p.info("Disposing event handler"),c.disposed=!0,f}p.info("Attaching event handler for",n);var c={type:n,callback:e,disposed:!1,context:o||window};v.listeners.push(c);var f={replay:r,dispose:i};return f}function t(n){p.info("Calling event handlers"),a++;try{n()}catch(n){p.error("UV API Error",n.stack)}a--,r()}function r(){if(0===u.length&&p.info("No more events to process"),u.length>0&&a>0&&p.info("Event will be processed later"),u.length>0&&0===a){p.info("Processing event");var n=u.shift();v.events.push(n),t(function(){s(v.listeners,function(e){if(!e.disposed&&l(e.type,n.meta.type))try{e.callback.call(e.context,n)}catch(n){p.error("Error emitting UV event",n.stack)}})})}}function i(n,e,o){var t=v.on(n,function(){e.apply(o||window,arguments),t.dispose()});return t}function s(n,e){for(var o=n.length,t=0;t<o;t++)e(n[t],t)}function c(n){var e={};for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o]);return e}function l(n,e){return"string"==typeof n?n===e:n.test(e)}function f(n,e){for(var o=n.length,t=[],r=0;r<o;r++)e(n[r])&&t.push(n[r]);return t}var u=[],a=0,p={info:function(){p.level>n.INFO||console&&console.info&&console.info.apply(console,arguments)},error:function(){p.level>n.ERROR||console&&console.error&&console.error.apply(console,arguments)}};n.ALL=0,n.INFO=1,n.ERROR=2,n.OFF=3,n(n.ERROR);var v={on:o,emit:e,once:i,events:[],listeners:[],logLevel:n};return v}"object"==typeof module&&module.exports?module.exports=n:window&&void 0===window.uv&&(window.uv=n())}();
</script>
Tested in IE8+, Firefox, Opera, Chrome, and Safari.
uv.emit(type, [data])
Emits an event with the type and data specified. The data should conform to the schema for the event type emitted. All events that are emitted are given a meta
property with the event type
.
uv.emit('ecProduct', {
product: {
id: '112-334-a',
price: 6.99,
name: '18th Birthday Baloon',
category: ['Party Accessories', 'Birthday Parties']
},
color: 'red',
stock: 6,
eventType: 'detail'
})
// => emits an ecProduct event
The emitted event will have meta attached.
{
"meta": {
"type": "ecProduct"
},
"product": {
"id": "112-334-a",
"price": 6.99,
"name": "18th Birthday Baloon",
"category": ["Party Accessories", "Birthday Parties"]
},
"color": "red",
"stock": 6,
"eventType": "detail"
}
uv.on(type, handler, [context])
This method attaches an event handler to be called when a certain event type is emitted. The handler will be passed the event data and will be bound to the context object, if one is passed. If a regex is passed, the handler will execute on events that match the regex.
uv.on('ecProduct', function (data) {
console.log(data)
})
// => logs data when an `ecProduct` event is emitted
var subscription = uv.on(/.*/, function (data) {
console.log(data)
})
// => logs data for all events
The on method returns a subscription object which can detach the handler using the dispose method and can also be used to replay events currently in the event array.
Note that subscriptions that have been disposed will not call the handler when replay is called. Let's look at an example:
subscription.dispose()
// => detatches the event handler
subscription.replay()
// => calls the handler for all events currently in uv.events
uv.once(type, handler, [context])
This method attaches an event handler that will be called once, on the next event emitted that matches the type specified. The handler will be passed the event data and will be bound to the context object, if one is passed.
It returns a subscription object which can detach the handler using the dispose method. If a regex is passed, the handler will execute on the next event to match the regex. Let's look at an example:
uv.once('ecProduct', function (data) {
console.log(data)
})
emit('ecProduct')
// => logs data
emit('ecProduct')
// => does not log
The events array is a cache of events emitted since the last page load. By iterating over the array it is possible to interpret the user journey or the current state of the page.
The uv-api is available on the NPM registry.
npm i uv-api --save
The module exports a createUv
function if required using CommonJS.
var createUv = require('uv-api')
var uv = createUv()
uv.emit('ecView')