openaire-library/utils/piwik/piwik.service.ts

79 lines
3.1 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {StringUtils} from '../string-utils.class';
import {EnvProperties} from '../properties/env-properties';
import {Observable} from "rxjs";
@Injectable()
export class PiwikService {
constructor(private http: HttpClient ) {}
trackViewForCustomUrl (properties:EnvProperties, title, pageparams, siteId = null):any {
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
if(typeof location !== 'undefined' && piwikId){
return this.doTrackView(properties,title,piwikId,location.href.split("?")[0] + "?" + pageparams);
}
}
trackView (properties:EnvProperties, title, siteId = null):any {
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
if(typeof location !== 'undefined' && piwikId){
return this.doTrackView(properties,title,piwikId,location.href);
}
}
private doTrackView (properties:EnvProperties, title, siteId, pageURL):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);
// return Observable.of(new Object()); // for testing
return this.http.get( url, {responseType: 'blob'});
// .do(request => console.info("Piwik request completed" ));
}
}
trackDownload (properties:EnvProperties, type = "", siteId = null):any {
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);
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){
}
}