@tangential/analytics
Version:
Analytics and advertising - monitization, generally.
206 lines (200 loc) • 8.36 kB
JavaScript
import * as i1 from '@tangential/core';
import { NgUtil } from '@tangential/core';
import * as i0 from '@angular/core';
import { Injectable, Component, Input, InjectionToken, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
const HitTypes = {
pageView: 'pageview',
screenView: 'screenview',
event: 'event',
transaction: 'transaction',
item: 'item',
social: 'social',
exception: 'exception',
timing: 'timing',
};
class GoogleAnalytics {
constructor(bus, logger, env) {
this.bus = bus;
this.logger = logger;
this.env = env;
this.analytics = (a, fields, other) => {
if (other) {
this.logger.trace(this, a, fields, other);
}
else {
this.logger.trace(this, a, fields);
}
};
let cfg = env.googleAnalytics;
if (cfg && cfg.enabled) {
this.awaitGoogle(5000).then(windowGa => {
this.analytics = windowGa;
this.create();
});
}
}
awaitGoogle(waitMils) {
return new Promise((resolve) => {
let started = Date.now();
let abortAt = started + waitMils;
let fn = () => {
if (window.ga) {
resolve(window.ga);
}
else if (Date.now() > abortAt) {
resolve(this.analytics);
}
else {
setTimeout(fn, 50);
}
};
});
}
create() {
this.analytics('create', this.env.googleAnalytics.trackingId, 'auto');
this.analytics('send', 'pageview');
}
navigatedToGeneric(state, route) {
const hit = {
hitType: HitTypes.pageView,
eventCategory: 'nav',
eventLabel: 'general'
};
this.analytics('set', 'page', hit.page || state.url);
this.analytics('send', 'generic');
}
navigatedTo(state, route, hit) {
hit.hitType = HitTypes.pageView;
this.analytics('set', 'page', hit.page || NgUtil.keylessUrl(state));
this.analytics('send', hit);
}
}
GoogleAnalytics.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: GoogleAnalytics, deps: [{ token: i1.MessageBus }, { token: i1.Logger }, { token: i1.AppEnvironment }], target: i0.ɵɵFactoryTarget.Injectable });
GoogleAnalytics.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: GoogleAnalytics });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: GoogleAnalytics, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: i1.MessageBus }, { type: i1.Logger }, { type: i1.AppEnvironment }]; } });
/* CommonModule required for ngStyle */
class AdsenseConfig {
constructor(config = {}) {
this.adClient = config.adClient || this.adClient;
this.adSlot = config.adSlot || this.adSlot;
this.adFormat = config.adFormat || this.adFormat;
this.display = config.display || 'block';
this.width = config.width;
this.height = config.height;
}
}
class AdsenseComponent {
constructor(config) {
this.config = config;
this.adFormat = 'auto';
this.adRegion = 'page-' + Math.floor(Math.random() * 10000) + 1;
this.pushed = false;
}
ngOnInit() {
this.adClient = this.adClient || this.config.adClient;
this.adSlot = this.adSlot || this.config.adSlot;
this.adFormat = this.adFormat || this.config.adFormat;
this.display = this.display || this.config.display;
this.width = this.width || this.config.width;
this.height = this.height || this.config.height;
}
ngAfterViewInit() {
// attempts to push the ad twice. Usually if one doesn't work the other
// will depending on if the browser has the adsense code cached
this.push();
if (!this.pushed) {
setTimeout(() => this.push(), 200);
}
}
push() {
try {
const adsByGoogle = window['adsbygoogle'];
adsByGoogle.push({});
this.pushed = true;
}
catch (e) {
console.warn('Could not load ads');
}
}
}
AdsenseComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: AdsenseComponent, deps: [{ token: AdsenseConfig }], target: i0.ɵɵFactoryTarget.Component });
AdsenseComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.2.10", type: AdsenseComponent, selector: "ng2-adsense", inputs: { adClient: "adClient", adSlot: "adSlot", adFormat: "adFormat", adRegion: "adRegion", display: "display", width: "width", height: "height" }, ngImport: i0, template: `
<div class="ng2-adsense">
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:50px"
[attr.data-ad-client]="adClient"
[attr.data-ad-slot]="adSlot"
[attr.data-ad-format]="adFormat"
[attr.data-ad-region]="adRegion"></ins>
</div>
`, isInline: true, styles: [".ng2-adsense{padding-bottom:8px}\n"] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: AdsenseComponent, decorators: [{
type: Component,
args: [{ selector: 'ng2-adsense', template: `
<div class="ng2-adsense">
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:50px"
[attr.data-ad-client]="adClient"
[attr.data-ad-slot]="adSlot"
[attr.data-ad-format]="adFormat"
[attr.data-ad-region]="adRegion"></ins>
</div>
`, styles: [".ng2-adsense{padding-bottom:8px}\n"] }]
}], ctorParameters: function () { return [{ type: AdsenseConfig }]; }, propDecorators: { adClient: [{
type: Input
}], adSlot: [{
type: Input
}], adFormat: [{
type: Input
}], adRegion: [{
type: Input
}], display: [{
type: Input
}], width: [{
type: Input
}], height: [{
type: Input
}] } });
const ADSENSE_CONFIG = new InjectionToken('AdsenseConfig');
function provideAdsenseConfig(config) {
return new AdsenseConfig(config);
}
class AdsenseModule {
static forRoot(config) {
return {
ngModule: AdsenseModule,
providers: [
{
provide: ADSENSE_CONFIG,
useValue: config
}, {
provide: AdsenseConfig,
useFactory: provideAdsenseConfig,
deps: [ADSENSE_CONFIG]
}
]
};
}
}
AdsenseModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: AdsenseModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
AdsenseModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.10", ngImport: i0, type: AdsenseModule, declarations: [AdsenseComponent], imports: [CommonModule], exports: [AdsenseComponent] });
AdsenseModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: AdsenseModule, imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.10", ngImport: i0, type: AdsenseModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule],
exports: [AdsenseComponent],
declarations: [AdsenseComponent],
}]
}] });
/*
* Public API Surface of analytics
*/
/**
* Generated bundle index. Do not edit.
*/
export { ADSENSE_CONFIG, AdsenseComponent, AdsenseConfig, AdsenseModule, GoogleAnalytics, HitTypes, provideAdsenseConfig };
//# sourceMappingURL=tangential-analytics.mjs.map