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

71 lines
2.7 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions, URLSearchParams} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import {Location} from '@angular/common';
import {StringUtils} from '../string-utils.class';
import {EnvProperties} from '../properties/env-properties';
@Injectable()
export class PiwikService {
private piwikbaseUrl:string = "";
constructor(private http: Http, private location: Location ) {}
trackView (properties:EnvProperties, title, siteId = null):any {
var ua = this.getUserAgent();
var referrer = this.getReferrer();
if(typeof location !== 'undefined'){
var url = properties.piwikBaseUrl+((siteId!=null)?siteId:properties.piwikSiteId)+"&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);
// .do(request => console.info("Piwik request completed" ));
}
}
trackDownload (properties:EnvProperties, downloadURL, 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(downloadURL)+"&download="+StringUtils.URIEncode(downloadURL)+
((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);
//.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){
}
}