ngx-pixel
Version:
An Angular library to simplify the use of a Facebook Pixel.
2 lines • 4.18 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/router"),require("@angular/common"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("ngx-pixel",["exports","@angular/core","@angular/router","@angular/common","rxjs/operators"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-pixel"]={},e.ng.core,e.ng.router,e.ng.common,e.rxjs.operators)}(this,(function(e,t,r,i,n){"use strict";var o=function(){function e(e,t,i,o,a){var s=this;this.config=e,this.injectedDocument=t,this.platformId=i,this.router=o,this.rendererFactory=a,this.doc=t,this.renderer=a.createRenderer(null,null),o&&o.events.pipe(n.filter((function(e){return e instanceof r.NavigationEnd}))).subscribe((function(e){s.isLoaded()&&s.track("PageView")}))}return e.prototype.initialize=function(e){void 0===e&&(e=this.config.pixelId),this.isLoaded()?console.warn("Tried to initialize a Pixel instance while another is already active. Please call `remove()` before initializing a new instance."):(this.config.enabled=!0,this.addPixelScript(e))},e.prototype.remove=function(){this.removePixelScript(),this.config.enabled=!1},e.prototype.track=function(e,t){i.isPlatformBrowser(this.platformId)&&(this.isLoaded()?t?fbq("track",e,t):fbq("track",e):console.warn("Tried to track an event without initializing a Pixel instance. Call `initialize()` first."))},e.prototype.trackCustom=function(e,t){i.isPlatformBrowser(this.platformId)&&(this.isLoaded()?t?fbq("trackCustom",e,t):fbq("trackCustom",e):console.warn("Tried to track an event without initializing a Pixel instance. Call `initialize()` first."))},e.prototype.addPixelScript=function(e){if(i.isPlatformBrowser(this.platformId)){var t="\n var pixelCode = function(f,b,e,v,n,t,s)\n {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n n.queue=[];t=b.createElement(e);t.async=!0;\n t.src=v;s=b.getElementsByTagName(e)[0];\n s.parentNode.insertBefore(t,s)}(window, document,'script',\n 'https://connect.facebook.net/en_US/fbevents.js');\n fbq('init', '"+e+"');\n fbq('track', 'PageView');",r=this.renderer.createElement("script");this.renderer.setAttribute(r,"id","pixel-script"),this.renderer.setAttribute(r,"type","text/javascript"),this.renderer.setProperty(r,"innerHTML",t),this.renderer.appendChild(this.doc.head,r)}},e.prototype.removePixelScript=function(){if(i.isPlatformBrowser(this.platformId)){var e=this.doc.getElementById("pixel-script");e&&e.remove()}},e.prototype.isLoaded=function(){return!!i.isPlatformBrowser(this.platformId)&&!!this.doc.getElementById("pixel-script")},e}();o.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new o(t.ɵɵinject("config"),t.ɵɵinject(i.DOCUMENT),t.ɵɵinject(t.PLATFORM_ID),t.ɵɵinject(r.Router,8),t.ɵɵinject(t.RendererFactory2))},token:o,providedIn:"root"}),o.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],o.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:["config"]}]},{type:void 0,decorators:[{type:t.Inject,args:[i.DOCUMENT]}]},{type:Object,decorators:[{type:t.Inject,args:[t.PLATFORM_ID]}]},{type:r.Router,decorators:[{type:t.Optional}]},{type:t.RendererFactory2}]};var a=function(){function e(t,r){if(this.pixel=t,!e.config)throw Error("ngx-pixel not configured correctly. Pass the `pixelId` property to the `forRoot()` function");e.config.enabled&&i.isPlatformBrowser(r)&&this.pixel.initialize()}return e.forRoot=function(t){this.config=t;var r=t.pixelId;return this.verifyPixelId(r),{ngModule:e,providers:[o,{provide:"config",useValue:t}]}},e.verifyPixelId=function(e){if(null==e||0===e.length)throw Error("Invalid Facebook Pixel ID. Did you pass the ID into the forRoot() function?")},e}();a.config=null,a.decorators=[{type:t.NgModule,args:[{imports:[]}]}],a.ctorParameters=function(){return[{type:o},{type:Object,decorators:[{type:t.Inject,args:[t.PLATFORM_ID]}]}]},e.PixelModule=a,e.PixelService=o,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-pixel.umd.min.js.map