import React from 'react'

export default Inner => class extends React.Component {
    render() {
        if (typeof(window) === 'undefined') return null;

        this.countly = window.Countly || {};
        this.countly.q = this.countly.q || [];

        const track = {
            userDetails: (o) => this.withCountly((countly) => countly.user_details(o)),
            pageview: (page) => this.withCountly((countly) => countly.track_pageview(page)),
            event: (event, data) => this.withCountly((countly) => countly.add_event(Object.assign({key: event}, data))),
        }
        return <Inner {...this.props} withCountly={(fn) => this.withCountly(fn)} track={track} />;
    }

    componentDidMount() {
        if (typeof(window) !== 'undefined' && !document.getElementById("cly_script")) {
            var clyScript = document.createElement("script");
            clyScript.id = "cly_script";
            clyScript.type = "text/javascript";
            clyScript.src = `${process.env.GATSBY_COUNTLY_URL}/sdk/web/countly.min.js`;
            clyScript.async = true;
            clyScript.onload = () => {
                window.Countly.q = this.countly.q;
                this.countly = window.Countly;
                this.countly.init({
                    app_key: process.env.GATSBY_COUNTLY_KEY,
                    url: process.env.GATSBY_COUNTLY_URL
                });
                this.countly.track_sessions();
            };
            document.body.appendChild(clyScript);
        }
    }

    withCountly(fn) {
        this.countly.q.push(() => fn(this.countly));
    }
}

@@ -95,159 +95,8 @@
 ) %7B%0A
-        if (typeof(window) === 'undefined') return null;%0A%0A        this.countly = window.Countly %7C%7C %7B%7D;%0A        this.countly.q = this.countly.q %7C%7C %5B%5D;%0A%0A
     
@@ -156,56 +156,33 @@
 his.
-withCountly((countly) =%3E countly.user_details(o)
+push('setUserId', o.email
 ),%0A 
@@ -216,67 +216,73 @@
  =%3E 
+%7B
 this.
-withCountly((countly) =%3E countly.
+push('setDocumentTitle', page);this.push('
 track
-_pageview(page))
+PageView');%7D
 ,%0A  
@@ -324,86 +324,54 @@
 his.
-withCountly((countly) =%3E countly.add_event(Object.assign(%7Bkey: event%7D, 
+push.apply(%5B'trackEvent', event%5D.concat(
 data))
-)
 ,%0A  
@@ -416,27 +416,20 @@
 .props%7D 
-withCountly
+push
 =%7B(fn) =
@@ -435,27 +435,20 @@
 =%3E this.
-withCountly
+push
 (fn)%7D tr
@@ -568,19 +568,22 @@
 ntById(%22
-cly
+matomo
 _script%22
@@ -603,19 +603,22 @@
     var 
-cly
+matomo
 Script =
@@ -660,27 +660,30 @@
             
-cly
+matomo
 Script.id = 
@@ -683,19 +683,22 @@
 t.id = %22
-cly
+matomo
 _script%22
@@ -707,27 +707,30 @@
             
-cly
+matomo
 Script.type 
@@ -758,27 +758,30 @@
             
-cly
+matomo
 Script.src =
@@ -807,40 +807,25 @@
 SBY_
-COUNTLY_URL%7D/sdk/web/countly.min
+MATOMO_URL%7D/piwik
 .js%60
@@ -834,27 +834,30 @@
             
-cly
+matomo
 Script.async
@@ -881,366 +881,195 @@
     
-clyScript.onload = () =%3E %7B%0A                window.Countly.q = this.countly.q;%0A                this.countly = window.Countly;%0A                this.countly.init(%7B%0A                    app_key: process.env.GATSBY_COUNTLY_KEY,%0A                    url: process.env.GATSBY_COUNTLY_URL%0A                %7D);%0A                this.countly.track_sessions();%0A            %7D
+this.push('setTrackerUrl', %60$%7Bprocess.env.GATSBY_MATOMO_URL%7D/piwik.php%60);%0A            this.push('setSiteId', process.env.GATSBY_MATOMO_SITE);%0A            this.push('enableHeartBeatTimer')
 ;%0A  
@@ -1108,11 +1108,14 @@
 ild(
-cly
+matomo
 Scri
@@ -1144,75 +1144,176 @@
     
-withCountly(fn) %7B%0A        this.countly.q.push(() =%3E fn(this.countly
+push() %7B%0A        if (typeof(window) === 'undefined') return null;%0A        window._paq = window._paq %7C%7C %5B%5D;%0A        window._paq.push(Array.prototype.slice.call(arguments
 ));%0A

Update stats