|
|
The current instance of `WebAnalytics` is stored inside `window._waInstance`.
|
|
|
|
|
|
WebAnalytics has a method called `sendEvent`:
|
|
|
|
|
|
```typescript
|
|
|
sendEvent(eventId: string, dataObject?: { [key: string]: string | number });
|
|
|
```
|
|
|
|
|
|
It can be called from anywhere the `WebAnalytics` instance can be reached, the `eventId` parameter is required.
|
|
|
|
|
|
In this example two events are defined, one triggered when a button identified with the css selector `'#primaryButton'` is clicked and its text content will be sent with it, the second one will be sent when the mouse pointer position will be at least 500px lower than the top of the page once, with the `dataObject` containing its horizontal position:
|
|
|
|
|
|
```js
|
|
|
(function() {
|
|
|
|
|
|
const webAnalytics = window._waInstance;
|
|
|
|
|
|
if (webAnalytics) {
|
|
|
|
|
|
// send event when primaryButton is clicked
|
|
|
const primaryButton = document.querySelector('#primaryButton');
|
|
|
primaryButton.onclick = (e) => {
|
|
|
webAnalytics.sendEvent('CUSTOM_EVENT_CLICK', { buttonText: e.target.textContent });
|
|
|
};
|
|
|
|
|
|
// send event when the mouse vertical position exceedes 500px (once)
|
|
|
var exceeded = false;
|
|
|
document.onmousemove = e => {
|
|
|
if (!exceeded && e.pageY > 500) {
|
|
|
|
|
|
exceeded = true;
|
|
|
|
|
|
webAnalytics.sendEvent('CUSTOM_EVENT_EXCEEDED_Y_500PX', { posX: e.pageX });
|
|
|
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
})();
|
|
|
``` |
|
|
\ No newline at end of file |