web-event-tracker-sdk
Version:
Auto-capture web events like page views, button clicks, and link clicks.
3 lines (2 loc) • 1.14 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).WebEventTracker=e()}(this,function(){"use strict";return function(){function t(){}return t.init=function(t){this.config=t,!1!==t.autoCapture&&this.setupAutoCapture(),this.capturePageView()},t.setupAutoCapture=function(){document.addEventListener("click",this.handleClick,!0)},t.handleClick=function(e){var n=e.target;if(n){var i="";i="A"===n.tagName?"link_click":"BUTTON"===n.tagName?"button_click":"element_click",t.sendEvent({type:i,tag:n.tagName,text:n.innerText||"",href:n.href||"",timestamp:Date.now()})}},t.capturePageView=function(){this.sendEvent({type:"page_view",url:window.location.href,title:document.title,timestamp:Date.now()})},t.sendEvent=function(t){this.config&&this.config.endpoint&&fetch(this.config.endpoint,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(this.config.apiKey)},body:JSON.stringify(t)}).catch(function(){})},t}()});
//# sourceMappingURL=web-event-tracker-sdk.umd.js.map