PageSourceSearch

https://www.danea.it/assets/js/url_parser.js

js danea.it collected 2026-10-01 10:40:56 UTC 4,208 bytes, 143 lines download raw bytes

1// questo metodo restituisce la configurazione delle regex e rispettive etichette da passare al dataLayer
2// se vanno aggiunte etichette:
3// - aggiungere un elemento all'array che sarà un altro array con in chiave 0 la regex e in chiave 1 l'etichetta
4// - la regex non va tra apicetti, l'etichetta si
5// - assicurarsi che gli slash ed eventualmente altri caratteri speciali vadano escapati con un backslash \
6// - questa è una piccola lista di caratteri speciali che devono essere escapati: . ^ $ * + - ? ( ) [ ] { } \ | / 
7// - NB: qunado la regex trova una corrispondenza esce quindi le ricerche più specifiche si devono trovare prima di quelle più generiche
8
9
10function get_prototype(){
11var data_layer_prototype=[];
12
13// software
14data_layer_prototype=[
15
16// contenuti-formazione
17[/\/formazione\//g,'contenuti-formazione'],
18[/\/webinars\//g,'contenuti-formazione'],
19[/\/corsi\//g,'contenuti-formazione'],
20
21// aree-supporto
22[/\/support\/faq\//g,'aree-supporto'],
23[/\/assistenza\//g,'aree-supporto'],
24[/\/support\//g,'aree-supporto'],
25[/\/supporto\//g,'aree-supporto'],
26
27
28// software
29
30[/\/software\/easyfatt\//g ,'software-easyfatt'],
31[/\/software\/fatture\-in\-cloud\//g,'software-fatture-in-cloud'],
32[/\/software\/domustudio\//g,'software-domustudio'],
33[/\/software\/numilia\//g,'software-numilia'],
34[/\/software\/altri\//g,'software-altri'],
35
36// download
37[/\/service\/download\//g,'download'],
38
39
40
41// carrello
42[/\/service\/shop\//g,'carrello'],
43
44// reseller
45[/\/reseller\//g,'reseller'],
46
47// land
48[/\/land\//g,'land'],
49
50
51
52]; // chiusura array
53
54
55
56return data_layer_prototype;
57}
58
59
60
61// This function creates a new anchor element and uses location
62// properties (inherent) to get the desired URL data. Some String
63// operations are used (to normalize results across browsers).
64 
65function parseURL(url) {
66    var a =  document.createElement('a');
67    a.href = url;
68    return {
69        source: url,
70        protocol: a.protocol.replace(':',''),
71        host: a.hostname,
72        port: a.port,
73        query: a.search,
74        params: (function(){
75            var ret = {},
76                seg = a.search.replace('/^?/','').split('&'),
77                len = seg.length, i = 0, s;
78            for (;i<len;i++) {
79                if (!seg[i]) { continue; }
80                s = seg[i].split('=');
81                ret[s[0]] = s[1];
82            }
83            return ret;
84        })(),
85        file: (a.pathname.match('//([^/?#]+)$/i') || [,''])[1],
86        hash: a.hash.replace('#',''),
87        path: a.pathname.replace('/^([^/])/','/$1'),
88        relative: (a.href.match('/tps?://[^/]+(.+)/') || [,''])[1],
89        segments: a.pathname.replace('/^//','').split('/')
90    };
91}
92
93function push_cat_to_data_layer(protot, url) {
94    //console.log(url);
95    var key;
96    var found =0;
97    for (key in protot) {
98        //console.log('dataLayer:',dataLayer);
99        //console.log(protot[key]);
100        //console.log(url.match(protot[key][0]));
101        //console.log(protot[key][0].test(url));
102        if(protot[key][0].test(url)===true){
103            dataLayer.push({
104            'pageCategory' : protot[key][1]
105            });
106         found=1;
107         break;   
108        } 
109    }
110    if(found==0 && url!="/"){
111        dataLayer.push({
112            'pageCategory' : 'altro-corporate'
113            });
114    }else if(url=="/"){// è homepage
115         dataLayer.push({
116            'pageCategory' : 'home-danea'
117            });
118    }
119  
120  }
121
122
123var myURL = parseURL(location.href);
124/* esempio di oggetto di ritorno:
125myURL.file;     // = 'index.html'
126myURL.hash;     // = 'top'
127myURL.host;     // = 'abc.com'
128myURL.query;    // = '?id=255&m=hello'
129myURL.params;   // = Object = { id: 255, m: hello }
130myURL.path;     // = '/dir/index.html'
131myURL.segments; // = Array = ['dir', 'index.html']
132myURL.port;     // = '8080'
133myURL.protocol; // = 'http'
134myURL.source;   // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
135*/
136
137var path=myURL.path.replace('/danea.it',''); // replace per ambiente locale
138
139//chiamo la funzione vera e propria:
140
141push_cat_to_data_layer(get_prototype(),path);
142
143

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.