Browse Source

add GoatCounter analytics

Son NK 5 years ago
parent
commit
35aa8f1438
2 changed files with 43 additions and 0 deletions
  1. 18 0
      server.py
  2. 25 0
      templates/base.html

+ 18 - 0
server.py

@@ -89,6 +89,7 @@ def create_app() -> Flask:
 
     init_admin(app)
     setup_paddle_callback(app)
+    setup_do_not_track(app)
 
     if FLASK_PROFILER_PATH:
         LOG.d("Enable flask-profiler")
@@ -401,6 +402,23 @@ def init_admin(app):
     admin.add_view(SLModelView(ClientUser, db.session))
 
 
+def setup_do_not_track(app):
+    @app.route("/dnt")
+    def do_not_track():
+        return """
+        <script>
+// Disable GoatCounter if this script is called
+
+window.localStorage.setItem('goatcounter-ignore', 't');
+
+alert("GoatCounter disabled");
+
+window.location.href = "/";
+
+</script>
+        """
+
+
 if __name__ == "__main__":
     app = create_app()
 

+ 25 - 0
templates/base.html

@@ -128,5 +128,30 @@
 {% block script %}
 {% endblock %}
 
+<script>
+  (function () {
+    // only enable on prod
+    if (!window.location.host.endsWith('simplelogin.io')) {
+      console.log("GoatCounter should only be enabled in prod");
+      return;
+    }
+
+    if (localStorage.getItem('goatcounter-ignore') === 't') {
+      console.log("GoatCounter is disabled");
+      return;
+    }
+
+    console.log("init GoatCounter");
+
+    var script = document.createElement('script');
+    window.counter = 'https://app.goatcounter.com/count'
+    script.async = 1;
+    script.src = '//gc.zgo.at/count.js';
+
+    var ins = document.getElementsByTagName('script')[0];
+    ins.parentNode.insertBefore(script, ins)
+  })();
+</script>
+
 </body>
 </html>