Piwik service: Change map to switchMap in order to return the observable.

This commit is contained in:
Konstantinos Triantafyllou 2023-07-12 16:24:23 +03:00
parent 37b7423e7f
commit 5d6fa99a73
1 changed files with 20 additions and 36 deletions

View File

@ -5,29 +5,29 @@ import {StringUtils} from '../string-utils.class';
import {EnvProperties} from '../properties/env-properties'; import {EnvProperties} from '../properties/env-properties';
import {Observable, of} from "rxjs"; import {Observable, of} from "rxjs";
import {ConfigurationService} from "../configuration/configuration.service"; import {ConfigurationService} from "../configuration/configuration.service";
import {map, take} from "rxjs/operators"; import {switchMap, take} from "rxjs/operators";
@Injectable() @Injectable()
export class PiwikService { export class PiwikService {
constructor(private http: HttpClient, private configurationService: ConfigurationService) { constructor(private http: HttpClient, private configurationService: ConfigurationService) {
} }
trackViewForCustomUrl(properties: EnvProperties, title, pageparams): any { trackViewForCustomUrl(properties: EnvProperties, title, pageparams): any {
if(typeof location !== 'undefined' && properties.enablePiwikTrack) { if (typeof location !== 'undefined' && properties.enablePiwikTrack) {
return this.configurationService.portalAsObservable.pipe(take(1), map(portal => { return this.configurationService.portalAsObservable.pipe(take(1), switchMap(portal => {
let piwik = portal?portal.piwik:null; let piwik = portal ? portal.piwik : null;
return this.doTrackView(properties, title, piwik, location.href.split("?")[0] + "?" + pageparams); return this.doTrackView(properties, title, piwik, location.href.split("?")[0] + "?" + pageparams);
})); }));
} else { } else {
return of(null); return of(null);
} }
} }
trackView(properties: EnvProperties, title): any { trackView(properties: EnvProperties, title) {
if(typeof location !== 'undefined' && properties.enablePiwikTrack) { if (typeof location !== 'undefined' && properties.enablePiwikTrack) {
return this.configurationService.portalAsObservable.pipe(take(1), map(portal => { return this.configurationService.portalAsObservable.pipe(take(1), switchMap(portal => {
let piwik = portal?portal.piwik:null; let piwik = portal ? portal.piwik : null;
return this.doTrackView(properties, title, piwik, location.href); return this.doTrackView(properties, title, piwik, location.href);
})); }));
} else { } else {
@ -36,9 +36,9 @@ export class PiwikService {
} }
trackDownload(properties: EnvProperties, type = "") { trackDownload(properties: EnvProperties, type = "") {
if(typeof location !== 'undefined' && properties.enablePiwikTrack) { if (typeof location !== 'undefined' && properties.enablePiwikTrack) {
return this.configurationService.portalAsObservable.pipe(take(1), map(portal => { return this.configurationService.portalAsObservable.pipe(take(1), switchMap(portal => {
let piwik = portal?portal.piwik:null; let piwik = portal ? portal.piwik : null;
return this.doTrackDownload(properties, type, piwik); return this.doTrackDownload(properties, type, piwik);
})); }));
} else { } else {
@ -46,20 +46,16 @@ export class PiwikService {
} }
} }
private doTrackView(properties: EnvProperties, title, siteId, pageURL): any { private doTrackView(properties: EnvProperties, title, siteId, pageURL): Observable<any> {
let ua = this.getUserAgent(); let ua = this.getUserAgent();
let referrer = this.getReferrer(); let referrer = this.getReferrer();
let piwikId = ((siteId != null) ? siteId : properties.piwikSiteId); let piwikId = ((siteId != null) ? siteId : properties.piwikSiteId);
if (typeof location !== 'undefined' && piwikId) { if (typeof location !== 'undefined' && piwikId) {
// console.log("Piwik - View: " + pageURL, title);
var url = properties.piwikBaseUrl + piwikId + "&rec=1&url=" + StringUtils.URIEncode(pageURL) + "&action_name=" + StringUtils.URIEncode(title) + var url = properties.piwikBaseUrl + piwikId + "&rec=1&url=" + StringUtils.URIEncode(pageURL) + "&action_name=" + StringUtils.URIEncode(title) +
((ua != null && ua.length > 0) ? ('&ua=' + StringUtils.URIEncode(ua)) : '') + ((ua != null && ua.length > 0) ? ('&ua=' + StringUtils.URIEncode(ua)) : '') +
((referrer != null && referrer.length > 0) ? ('&urlref=' + StringUtils.URIEncode(referrer)) : ''); ((referrer != null && referrer.length > 0) ? ('&urlref=' + StringUtils.URIEncode(referrer)) : '');
// console.log("Piwik - View: " + url);
// return Observable.of(new Object()); // for testing // return Observable.of(new Object()); // for testing
return this.http.get(url, {responseType: 'blob'}); return this.http.get(url, {responseType: 'blob'});
// .do(request => console.info("Piwik request completed" ));
} }
} }
@ -67,40 +63,28 @@ export class PiwikService {
var ua = this.getUserAgent(); var ua = this.getUserAgent();
var referrer = this.getReferrer(); var referrer = this.getReferrer();
var url = properties.piwikBaseUrl + ((siteId != null) ? siteId : properties.piwikSiteId) + "&rec=1&url=" + StringUtils.URIEncode(location.href) + "&download=" + StringUtils.URIEncode(location.href + "#" + type) + var url = properties.piwikBaseUrl + ((siteId != null) ? siteId : properties.piwikSiteId) + "&rec=1&url=" + StringUtils.URIEncode(location.href) + "&download=" + StringUtils.URIEncode(location.href + "#" + type) +
((ua != null && ua.length > 0) ? ('&ua=' + StringUtils.URIEncode(ua)) : '') + ((ua != null && ua.length > 0) ? ('&ua=' + StringUtils.URIEncode(ua)) : '') +
((referrer != null && referrer.length > 0) ? ('&urlref=' + StringUtils.URIEncode(referrer)) : ''); ((referrer != null && referrer.length > 0) ? ('&urlref=' + StringUtils.URIEncode(referrer)) : '');
//console.log("Piwik - trackDownload: "+url);
return this.http.get(url, {responseType: 'blob'}); return this.http.get(url, {responseType: 'blob'});
//.do(request => console.info("Piwik request completed" ));
} }
private getUserAgent() { private getUserAgent() {
if (typeof navigator !== 'undefined') { if (typeof navigator !== 'undefined') {
//console.log("navigator.userAgent:" + navigator.userAgent);
return navigator.userAgent; return navigator.userAgent;
} else { } else {
return null; return null;
} }
} }
private getReferrer() { private getReferrer() {
var referrer = ""; var referrer = "";
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
//console.log("document.referrer:" + document.referrer);
referrer = document.referrer; referrer = document.referrer;
} }
if ((referrer == null || referrer.length == 0) && typeof localStorage !== 'undefined') { if ((referrer == null || referrer.length == 0) && typeof localStorage !== 'undefined') {
referrer = localStorage.getItem('previousRoute'); referrer = localStorage.getItem('previousRoute');
} }
return referrer; return referrer;
} }
parse(data: any) {
}
} }