diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go
index 3690784375d..c0e27897ff4 100644
--- a/pkg/api/frontendsettings.go
+++ b/pkg/api/frontendsettings.go
@@ -141,6 +141,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
"authProxyEnabled": setting.AuthProxyEnabled,
"ldapEnabled": setting.LdapEnabled,
"alertingEnabled": setting.AlertingEnabled,
+ "googleAnalyticsId": setting.GoogleAnalyticsId,
"buildInfo": map[string]interface{}{
"version": setting.BuildVersion,
"commit": setting.BuildCommit,
diff --git a/public/app/core/services/analytics.js b/public/app/core/services/analytics.js
index 982cdd23d1d..a20a11bb2b6 100644
--- a/public/app/core/services/analytics.js
+++ b/public/app/core/services/analytics.js
@@ -1,27 +1,39 @@
define([
'angular',
- '../core_module',
+ 'jquery',
+ 'app/core/core_module',
+ 'app/core/config',
],
-function(angular, coreModule) {
+function(angular, $, coreModule, config) {
'use strict';
coreModule.default.service('googleAnalyticsSrv', function($rootScope, $location) {
- var first = true;
+
+ function gaInit() {
+ $.getScript('https://www.google-analytics.com/analytics.js'); // jQuery shortcut
+ var ga = window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments); }; ga.l = +new Date;
+ ga('create', config.googleAnalyticsId, 'auto');
+ return ga;
+ }
this.init = function() {
+
$rootScope.$on('$viewContentLoaded', function() {
- // skip first
- if (first) {
- first = false;
- return;
- }
- window.ga('send', 'pageview', { page: $location.url() });
+ var track = { page: $location.url() };
+
+ var ga = window.ga || gaInit();
+
+ ga('set', track);
+ ga('send', 'pageview');
});
+
};
}).run(function(googleAnalyticsSrv) {
- if (window.ga) {
+
+ if (config.googleAnalyticsId) {
googleAnalyticsSrv.init();
}
+
});
});
diff --git a/public/views/index.html b/public/views/index.html
index 8d231ed2b68..55fe9408a73 100644
--- a/public/views/index.html
+++ b/public/views/index.html
@@ -91,18 +91,6 @@
- [[if .GoogleAnalyticsId]]
-
- [[end]]
-
[[if .GoogleTagManagerId]]