1/**
2 * Set up Google analytics
3 *
4 * All configuration is done in the JSINFO.ga object initialized in
5 * action.php
6 */
7if (JSINFO.ga) {
8    if (JSINFO.ga.gtagId) {
9        /* Global site tag (gtag.js) - Google Analytics */
10
11        var script = document.createElement('script');
12        script.type = 'text/javascript';
13        script.src = 'https://www.googletagmanager.com/gtag/js?id=' + JSINFO.ga.gtagId;
14
15        document.body.appendChild(script);
16
17        window.dataLayer = window.dataLayer || [];
18
19        function gtag() {
20            dataLayer.push(arguments);
21        }
22
23        gtag('js', new Date());
24        gtag('config', JSINFO.ga.gtagId);
25    } else {
26        /* default google tracking initialization */
27        (function(i, s, o, g, r, a, m) {
28            i['GoogleAnalyticsObject'] = r;
29            //noinspection CommaExpressionJS
30            i[r] = i[r] || function() {
31                (i[r].q = i[r].q || []).push(arguments)
32            }, i[r].l = 1 * new Date();
33            //noinspection CommaExpressionJS
34            a = s.createElement(o),
35                m = s.getElementsByTagName(o)[0];
36            a.async = 1;
37            a.src = g;
38            m.parentNode.insertBefore(a, m)
39        })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
40
41        // initalize and set options
42        ga('create', JSINFO.ga.trackingId, 'auto', JSINFO.ga.options);
43        ga('set', 'forceSSL', true);
44        ga('set', 'anonymizeIp', JSINFO.ga.anonymizeIp);
45
46        // track pageview and action
47        ga('set', 'dimension1', JSINFO.ga.action);
48        ga('set', 'dimension2', JSINFO.ga.id);
49        ga('send', 'pageview', JSINFO.ga.pageview);
50        ga('send', 'event', 'wiki-action', JSINFO.ga.action, JSINFO.id, {
51            nonInteraction: true // this is an automatic event with the page load
52        });
53
54        // The hit payload is also send to the console
55        // https://developers.google.com/analytics/devguides/collection/analyticsjs/tasks#overriding_a_task
56        if (JSINFO.ga.debug) {
57
58            ga(function(tracker) {
59
60                // Grab a reference to the default sendHitTask function.
61                var originalSendHitTask = tracker.get('sendHitTask');
62
63                // Overwrite and add an output to the console
64                tracker.set('sendHitTask', function(model) {
65                    originalSendHitTask(model);
66                    console.log("Doku Google Analytics Plugin Debug: Hit Payload: " + model.get('hitPayload'));
67                });
68
69            });
70
71        }
72
73        // track outgoing links, once the document was loaded
74        if (JSINFO.ga.trackOutboundLinks) {
75
76            jQuery(function() {
77                // https://support.google.com/analytics/answer/1136920?hl=en
78                jQuery('a.urlextern, a.interwiki').click(function() {
79                    var url = this.href;
80                    if (ga && ga.loaded) {
81                        ga('send', 'event', 'outbound', 'click', url, {
82                            'transport': 'beacon'
83                        });
84                    }
85                });
86            });
87
88        }
89    }
90}