ngx-hotjar
Version:
A simple ng-6 wrapper to load hotjar dependency by angular way
181 lines (173 loc) • 4.97 kB
JavaScript
import { __awaiter } from 'tslib';
import { InjectionToken, APP_INITIALIZER, isDevMode, Injectable, NgModule, defineInjectable } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/** @type {?} */
const NGX_HOTJAR_SETTINGS_TOKEN = new InjectionToken('ngx-hotjar-settings', {
factory: () => ({ trackingCode: '', version: 6 })
});
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/** @type {?} */
const NGX_HOTJAR_INITIALIZER_PROVIDER = {
provide: APP_INITIALIZER,
multi: true,
useFactory: HotjarInitializer,
deps: [
NGX_HOTJAR_SETTINGS_TOKEN
]
};
/**
* @param {?} $settings
* @return {?}
*/
function HotjarInitializer($settings) {
return () => __awaiter(this, void 0, void 0, function* () {
if (!$settings.trackingCode) {
if (!isDevMode()) {
console.error('Empty tracking code for Hotjar. Make sure to provide one when initializing NgxHotjarModule.');
}
return;
}
(function (h, o, t, j, a, r) {
h.hj = h.hj || function () {
(h.hj.q = h.hj.q || []).push(arguments);
};
h._hjSettings = { hjid: $settings.trackingCode, hjsv: $settings.version || 6 };
a = o.getElementsByTagName('head')[0];
r = o.createElement('script');
r.async = 1;
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
a.appendChild(r);
})(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
class NgxHotjarService {
constructor() { }
/**
* Fires an PageView event to an virtual url path
*
* @param {?} path virtual url
* @return {?}
*/
virtualPageView(path) {
try {
hj('vpv', path);
}
catch (err) {
this.error(err);
}
}
/**
* Fires an event on Hotjar. Use this method to trigger events on forms and start video recordings.
*
* @param {?} path url
* @return {?}
*/
trigger(path) {
try {
hj('trigger', path);
}
catch (err) {
this.error(err);
}
}
/**
* Allows you to tag recordings on Hotjar of all visitors passing through a page.
*
* @param {?} path tags
* @return {?}
*/
tagRecording(path) {
try {
hj('tagRecording', path);
}
catch (err) {
this.error(err);
}
}
/**
* This option is available in case you need to set up page change tracking manually
* within your app's router.
*
* @param {?} path Path
* @return {?}
*/
stateChange(path) {
try {
hj('stateChange', path);
}
catch (err) {
this.error(err);
}
}
/**
* @param {?} err
* @return {?}
*/
error(err) {
// window.hj=window.hj||function(){(hj.q=hj.q||[]).push(arguments)};
/** @todo Check typef for Hotjar */
throw new Error('Hotjar is not loaded');
}
}
NgxHotjarService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgxHotjarService.ctorParameters = () => [];
/** @nocollapse */ NgxHotjarService.ngInjectableDef = defineInjectable({ factory: function NgxHotjarService_Factory() { return new NgxHotjarService(); }, token: NgxHotjarService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
class NgxHotjarModule {
/**
* @param {?} trackingCode
* @param {?=} version
* @return {?}
*/
static forRoot(trackingCode, version = 6) {
return {
ngModule: NgxHotjarModule,
providers: [
{
provide: NGX_HOTJAR_SETTINGS_TOKEN,
useValue: {
trackingCode: trackingCode,
version: version
}
},
NGX_HOTJAR_INITIALIZER_PROVIDER,
]
};
}
}
NgxHotjarModule.decorators = [
{ type: NgModule, args: [{
imports: [],
declarations: [],
exports: [],
providers: []
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
export { HotjarInitializer, NGX_HOTJAR_INITIALIZER_PROVIDER, NgxHotjarService, NGX_HOTJAR_SETTINGS_TOKEN, NgxHotjarModule };
//# sourceMappingURL=ngx-hotjar.js.map