1 |
61381
|
k.triantaf
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpClient} from "@angular/common/http";
|
3 |
|
|
|
4 |
|
|
import {StringUtils} from '../string-utils.class';
|
5 |
|
|
import {EnvProperties} from '../properties/env-properties';
|
6 |
|
|
import {Observable} from "rxjs";
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
@Injectable()
|
10 |
|
|
export class PiwikService {
|
11 |
|
|
constructor(private http: HttpClient ) {}
|
12 |
|
|
trackViewForCustomUrl (properties:EnvProperties, title, pageparams, siteId = null):any {
|
13 |
|
|
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
|
14 |
|
|
if(typeof location !== 'undefined' && piwikId){
|
15 |
|
|
return this.doTrackView(properties,title,piwikId,location.href.split("?")[0] + "?" + pageparams);
|
16 |
|
|
}
|
17 |
|
|
}
|
18 |
|
|
trackView (properties:EnvProperties, title, siteId = null):any {
|
19 |
|
|
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
|
20 |
|
|
if(typeof location !== 'undefined' && piwikId){
|
21 |
|
|
return this.doTrackView(properties,title,piwikId,location.href);
|
22 |
|
|
}
|
23 |
|
|
}
|
24 |
|
|
private doTrackView (properties:EnvProperties, title, siteId, pageURL):any {
|
25 |
|
|
|
26 |
|
|
let ua = this.getUserAgent();
|
27 |
|
|
let referrer = this.getReferrer();
|
28 |
|
|
let piwikId= ((siteId!=null)?siteId:properties.piwikSiteId);
|
29 |
|
|
if(typeof location !== 'undefined' && piwikId){
|
30 |
|
|
// console.log("Piwik - View: " + pageURL, title);
|
31 |
|
|
var url = properties.piwikBaseUrl+piwikId+"&rec=1&url="+StringUtils.URIEncode(pageURL)+"&action_name="+StringUtils.URIEncode(title)+
|
32 |
|
|
((ua != null && ua.length > 0)?('&ua='+StringUtils.URIEncode(ua)):'')+
|
33 |
|
|
((referrer != null && referrer.length > 0)?('&urlref='+StringUtils.URIEncode(referrer)):'');
|
34 |
|
|
// console.log("Piwik - View: " + url);
|
35 |
|
|
// return Observable.of(new Object()); // for testing
|
36 |
|
|
return this.http.get( url, {responseType: 'blob'});
|
37 |
|
|
// .do(request => console.info("Piwik request completed" ));
|
38 |
|
|
|
39 |
|
|
}
|
40 |
|
|
}
|
41 |
|
|
trackDownload (properties:EnvProperties, type = "", siteId = null):any {
|
42 |
|
|
var ua = this.getUserAgent();
|
43 |
|
|
var referrer = this.getReferrer();
|
44 |
|
|
var url = properties.piwikBaseUrl+((siteId!=null)?siteId:properties.piwikSiteId)+"&rec=1&url="+StringUtils.URIEncode(location.href)+"&download="+StringUtils.URIEncode(location.href+"#"+type)+
|
45 |
|
|
((ua != null && ua.length > 0)?('&ua='+StringUtils.URIEncode(ua)):'')+
|
46 |
|
|
((referrer != null && referrer.length > 0)?('&urlref='+StringUtils.URIEncode(referrer)):'');
|
47 |
|
|
//console.log("Piwik - trackDownload: "+url);
|
48 |
|
|
|
49 |
|
|
return this.http.get( url, {responseType: 'blob'});
|
50 |
|
|
//.do(request => console.info("Piwik request completed" ));
|
51 |
|
|
|
52 |
|
|
}
|
53 |
|
|
private getUserAgent(){
|
54 |
|
|
if (typeof navigator !== 'undefined') {
|
55 |
|
|
//console.log("navigator.userAgent:" + navigator.userAgent);
|
56 |
|
|
return navigator.userAgent;
|
57 |
|
|
|
58 |
|
|
}else{
|
59 |
|
|
return null;
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
private getReferrer(){
|
63 |
|
|
var referrer = "";
|
64 |
|
|
if (typeof document !== 'undefined') {
|
65 |
|
|
//console.log("document.referrer:" + document.referrer);
|
66 |
|
|
referrer = document.referrer;
|
67 |
|
|
|
68 |
|
|
}
|
69 |
|
|
if((referrer == null || referrer.length == 0)&&typeof localStorage !== 'undefined'){
|
70 |
|
|
|
71 |
|
|
referrer =localStorage.getItem('previousRoute');
|
72 |
|
|
}
|
73 |
|
|
return referrer;
|
74 |
|
|
}
|
75 |
|
|
parse(data:any){
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
}
|