Просмотр исходного кода

Added googla analytics id setting

Torkel Ödegaard 10 лет назад
Родитель
Сommit
80c771c945
7 измененных файлов с 72 добавлено и 15 удалено
  1. 11 7
      conf/defaults.ini
  2. 9 6
      conf/sample.ini
  3. 4 0
      pkg/api/index.go
  4. 5 2
      pkg/setting/setting.go
  5. 1 0
      src/app/services/all.js
  6. 28 0
      src/app/services/analytics.js
  7. 14 0
      src/views/index.html

+ 11 - 7
conf/defaults.ini

@@ -1,12 +1,6 @@
 app_name = Grafana
 app_mode = production
 
-# Report anonymous usage counters to stats.grafana.org (https).
-# No ip addresses are being tracked, only simple counters to track
-# running instances, dashboard and error counts. It is very helpful to us.
-# Change this option to false to disable reporting.
-reporting-enabled = true
-
 [server]
 ; protocol (http or https)
 protocol = http
@@ -21,11 +15,21 @@ root_url = %(protocol)s://%(domain)s:%(http_port)s/
 router_logging = false
 ; the path relative to the binary where the static (html/js/css) files are placed
 static_root_path = public
+; enable gzip
 enable_gzip = false
-; if https protocol
+; https certs & key file
 cert_file =
 cert_key =
 
+[analytics]
+# Server reporting, sends usage counters to stats.grafana.org (https).
+# No ip addresses are being tracked, only simple counters to track
+# running instances, dashboard and error counts. It is very helpful to us.
+# Change this option to false to disable reporting.
+reporting_enabled = true
+; Google Analytics universal tracking code, only enabled if you specify an id here
+google_analytics_ua_id =
+
 [database]
 ; Either "mysql", "postgres" or "sqlite3", it's your choice
 type = sqlite3

+ 9 - 6
conf/sample.ini

@@ -5,12 +5,6 @@
 
 app_mode = production
 
-# Report anonymous usage counters to stats.grafana.org (https).
-# No ip addresses are being tracked, only simple counters to track
-# running instances, dashboard and error counts. It is very helpful to us.
-# Change this option to false to disable reporting.
-reporting-enabled = true
-
 [server]
 ; protocol (http or https)
 protocol = http
@@ -27,6 +21,15 @@ router_logging = false
 static_root_path = public
 enable_gzip = false
 
+[analytics]
+# Server reporting, sends usage counters to stats.grafana.org (https).
+# No ip addresses are being tracked, only simple counters to track
+# running instances, dashboard and error counts. It is very helpful to us.
+# Change this option to false to disable reporting.
+reporting_enabled = true
+; Google Analytics universal tracking code, only enabled if you specify an id here
+google_analytics_ua_id =
+
 [database]
 ; Either "mysql", "postgres" or "sqlite3", it's your choice
 type = sqlite3

+ 4 - 0
pkg/api/index.go

@@ -33,6 +33,10 @@ func setIndexViewData(c *middleware.Context) error {
 	c.Data["AppUrl"] = setting.AppUrl
 	c.Data["AppSubUrl"] = setting.AppSubUrl
 
+	if setting.GoogleAnalyticsId != "" {
+		c.Data["GoogleAnalyticsId"] = setting.GoogleAnalyticsId
+	}
+
 	return nil
 }
 

+ 5 - 2
pkg/setting/setting.go

@@ -97,7 +97,8 @@ var (
 
 	configFiles []string
 
-	ReportingEnabled bool
+	ReportingEnabled  bool
+	GoogleAnalyticsId string
 )
 
 func init() {
@@ -235,7 +236,9 @@ func NewConfigContext(config string) {
 	ImagesDir = "data/png"
 	PhantomDir = "vendor/phantomjs"
 
-	ReportingEnabled = Cfg.Section("").Key("reporting-enabled").MustBool(true)
+	analytics := Cfg.Section("analytics")
+	ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true)
+	GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
 
 	readSessionConfig()
 }

+ 1 - 0
src/app/services/all.js

@@ -5,6 +5,7 @@ define([
   './contextSrv',
   './timer',
   './keyboardManager',
+  './analytics',
   './popoverSrv',
   './backendSrv',
 ],

+ 28 - 0
src/app/services/analytics.js

@@ -0,0 +1,28 @@
+define([
+  'angular',
+],
+function(angular) {
+  'use strict';
+
+  var module = angular.module('grafana.services');
+  module.service('googleAnalyticsSrv', function($rootScope, $location) {
+
+    var first = true;
+
+    this.init = function() {
+      $rootScope.$on('$viewContentLoaded', function() {
+        // skip first
+        if (first) {
+          first = false;
+          return;
+        }
+        window.ga('send', 'pageview', { page: $location.url() });
+      });
+    };
+
+  }).run(function(googleAnalyticsSrv) {
+    if (window.ga) {
+      googleAnalyticsSrv.init();
+    }
+  });
+});

+ 14 - 0
src/views/index.html

@@ -59,5 +59,19 @@
     require(['app'], function (app) {
 	    app.boot();
     })
+
 	</script>
+
+	[[if .GoogleAnalyticsId]]
+		<script>
+		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+		(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+		m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+		})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+		ga('create', '[[.GoogleAnalyticsId]]', 'auto');
+		ga('send', 'pageview');
+		</script>
+	[[end]]
+
 </html>