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

66 lines
2.5 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {StringUtils} from '../string-utils.class';
import {EnvProperties} from '../properties/env-properties';
@Injectable()
export class PiwikService {
constructor(private http: HttpClient ) {}
trackView (properties:EnvProperties, title, siteId = null):any {
let ua = this.getUserAgent();
let referrer = this.getReferrer();
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
if(typeof location !== 'undefined' && piwikId){
var url = properties.piwikBaseUrl+piwikId+"&rec=1&url="+StringUtils.URIEncode(location.href)+"&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()).mapTo(true); // 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){
}
}