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