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

71 lines
2.5 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 {OpenaireProperties} from '../properties/openaireProperties';
import {StringUtils} from '../string-utils.class';
@Injectable()
export class PiwikService {
private piwikbaseUrl:string = "";
constructor(private http: Http, private location: Location ) {}
trackView (title):any {
var ua = this.getUserAgent();
var referrer = this.getReferrer();
if(typeof location !== 'undefined'){
var url = OpenaireProperties.getPiwikBaseURL()+"&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 this.http.get( url);
// .do(request => console.info("Piwik request completed" ));
}
}
trackDownload (downloadURL):any {
var ua = this.getUserAgent();
var referrer = this.getReferrer();
var url = OpenaireProperties.getPiwikBaseURL()+"&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){
}
}