File size: 4,934 Bytes
e852054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

if ('undefined' === (typeof piwikUsageTracking) || !piwikUsageTracking) {
    piwikUsageTracking = {targets: [], visitorCustomVariables: [], trackingDomain: '', exampleDomain: ''};
}

piwikUsageTracking.trackers = [];
piwikUsageTracking.initialized = false;
piwikUsageTracking.createTrackersIfNeeded = function ()
{
    if (piwikUsageTracking.initialized) {
        return;
    }

    piwikUsageTracking.trackers = [];
    piwikUsageTracking.initialized = true;

    if (!piwikUsageTracking.targets || !piwikUsageTracking.targets.length) {
        return;
    }

    var isHttps = location.protocol == "https:";

    $.each(piwikUsageTracking.targets, function (index, target) {
        if ('undefined' === (typeof Piwik)) {
            // blocked by ad blocker
            return;
        }

        if (isHttps && 0 === target.url.indexOf('http:')) {
            target.url = 'https://' + target.url.substring(location.protocol.length);
        }

        var tracker = Piwik.getTracker(target.url, target.idSite);
            tracker.isAnon = target.useAnonymization;
        // we could do those calls later via `paq.push` but I want to make sure those methods are called and anonymized
        // eg if there was a typo in `setDocumentTitle` we would not notice the method is not executed otherwise and
        // it would result in a not anonymized title

        if (!target.useAnonymization) {
            tracker.setUserId(piwikUsageTracking.userId);
        }

        anonymizeReferrer(tracker);
        anonymizeTitle(tracker);
        anonymizeUrl(tracker);


        piwikUsageTracking.trackers.push(tracker);
    });

    // we cannot enable link tracking or JS error tracking as it is too likely that custom data is tracked
    $.each(piwikUsageTracking.visitorCustomVariables, function (j, customVar) {
        _paq.push(['setCustomVariable', customVar.id, customVar.name, customVar.value]);
    });

    trackPageView();

    function ucfirst(text)
    {
        var firstLetter = ('' + text).charAt(0).toUpperCase();
        return firstLetter + text.substr(1);
    }

    function createUrlAnonymizer()
    {
        return new UrlAnonymizer(location.href);
    }

    function anonymizeTitle(tracker)
    {
        if (!tracker.isAnon) return;

        var urlAnonymizer = createUrlAnonymizer();
        var module  = urlAnonymizer.getTopLevelId()
        var action  = urlAnonymizer.getSubLevelId();

        var title = 'Matomo Web Analytics';

        if (module) {
            title = ucfirst(module);

            if (action) {
                title += ' ' + ucfirst(action);
            }
        } else if ($('#login_form').length) {
            // this is the case when eg opening http://example.matomo.org/ and one is not logged in (we show login page)
            title = 'Login';
        }

        tracker.setDocumentTitle(title);
    }

    function anonymizeReferrer(tracker)
    {
        if (!tracker.isAnon) return;

        tracker.setReferrerUrl('');
    }

    function anonymizeUrl(tracker)
    {
        if (!tracker.isAnon) return;

        var urlAnonymizer = createUrlAnonymizer();
        var url = urlAnonymizer.getAnonymizedUrl();
        url = urlAnonymizer.makeUrlHierarchical(url);

        tracker.setCustomUrl(url);
    }

    function trackPageView()
    {
        var urlAnonymizer = createUrlAnonymizer();
        var popover = urlAnonymizer.getPopoverNameFromUrl();

        (piwikUsageTracking.trackers || []).forEach(anonymizeTitle);
        (piwikUsageTracking.trackers || []).forEach(anonymizeUrl);

        if (popover) {
            _paq.push(['setCustomVariable', 1, 'popover', popover, 'page']);
        }

        _paq.push(['trackPageView']);
    }

    $(function () {
        window.CoreHome.Matomo.on('matomoPageChange', function () {
            trackPageView();
        });
    });
};

var _paq = {
    push: function() {
        piwikUsageTracking.createTrackersIfNeeded();

        // push a method to all available trackers
        $.each(arguments, function (i, parameterArray) {
            var method = parameterArray.shift();

            $.each(piwikUsageTracking.trackers, function (index, tracker) {
                // the users piwik might not support a method yet, only execute if it exists
                if (typeof method === 'string' && tracker[method]) {
                    tracker[method].apply(tracker, parameterArray);
                } else if (typeof method === 'function') {
                    method.apply(tracker, parameterArray);
                }
            });
        });
    }
};

window.addEventListener('DOMContentLoaded', function () {
    if (piwikUsageTracking && piwikUsageTracking.createTrackersIfNeeded) {
        piwikUsageTracking.createTrackersIfNeeded();
    }
});