Skip to content

Instantly share code, notes, and snippets.

@gbechtold
Created September 27, 2014 10:24
Show Gist options
  • Save gbechtold/907dbaa5709fde3d1632 to your computer and use it in GitHub Desktop.
Save gbechtold/907dbaa5709fde3d1632 to your computer and use it in GitHub Desktop.
diff --git a/app/index.html b/app/index.html
index 09c05ae..900e8fc 100644
--- a/app/index.html
+++ b/app/index.html
@@ -92,16 +92,7 @@
</div>
<!-- Begin Analytics-->
-<!-- Google Analytics Start -->
-<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', 'UA-46278040-1', 'auto');
- ga('send', 'pageview');
-</script>
-<!-- Google Analytics End -->
+<!-- Standard Google Analytics replaced with AngularJS Service -->
<!-- Crate UDC -->
<script>
diff --git a/app/scripts/app.js b/app/scripts/app.js
index b73ff13..d277fd5 100644
--- a/app/scripts/app.js
+++ b/app/scripts/app.js
@@ -14,6 +14,7 @@ var crateAdminApp = angular.module('crateAdminApp', [
'cluster',
'tableinfo',
'nodeinfo'
+ 'analytics'
]);
crateAdminApp.config(['$routeProvider', '$httpProvider',
diff --git a/app/scripts/controllers/cluster.js b/app/scripts/controllers/cluster.js
index feab27b..715a30f 100644
--- a/app/scripts/controllers/cluster.js
+++ b/app/scripts/controllers/cluster.js
@@ -2,7 +2,7 @@
angular.module('cluster', ['stats', 'sql', 'common', 'nodeinfo'])
.controller('NodeListController', function($scope, $route,
- ClusterState, prepareNodeList, NodeHealth, NodeListInfo, compareByHealth){
+ ClusterState, prepareNodeList, NodeHealth, NodeListInfo, compareByHealth, analytics){
$scope.nodes = [];
$scope.selected = null;
diff --git a/app/scripts/controllers/console.js b/app/scripts/controllers/console.js
index d7b4c57..7ef68fb 100644
--- a/app/scripts/controllers/console.js
+++ b/app/scripts/controllers/console.js
@@ -1,7 +1,7 @@
'use strict';
angular.module('console', ['sql'])
- .controller('ConsoleController', function ($scope, $http, $location, SQLQuery, $log, $timeout) {
+ .controller('ConsoleController', function ($scope, $http, $location, SQLQuery, $log, $timeout, analytics) {
$scope.statement = "";
$scope.typedStatement = "";
diff --git a/app/scripts/controllers/feed.js b/app/scripts/controllers/feed.js
index fbe062d..ef3aff6 100644
--- a/app/scripts/controllers/feed.js
+++ b/app/scripts/controllers/feed.js
@@ -1,7 +1,7 @@
'use strict';
angular.module('feed', ['stats'])
- .factory('FeedService', function($http){
+ .factory('FeedService', function($http, analytics){
var FEED_SUFFIX = '/feed/json?callback=JSON_CALLBACK';
return {
parse: function(url) {
diff --git a/app/scripts/controllers/overview.js b/app/scripts/controllers/overview.js
index 4d3f3b5..0acfda3 100644
--- a/app/scripts/controllers/overview.js
+++ b/app/scripts/controllers/overview.js
@@ -1,7 +1,7 @@
'use strict';
angular.module('overview', ['stats'])
- .controller('OverviewController', function ($scope, $location, $log, $timeout, ClusterState) {
+ .controller('OverviewController', function ($scope, $location, $log, $timeout, ClusterState, analytics) {
var colorMap = {
"good": 'panel-success',
diff --git a/app/scripts/controllers/tables.js b/app/scripts/controllers/tables.js
index 9cca7ed..93303c5 100644
--- a/app/scripts/controllers/tables.js
+++ b/app/scripts/controllers/tables.js
@@ -1,7 +1,7 @@
'use strict';
angular.module('tables', ['stats', 'sql', 'common', 'tableinfo'])
- .provider('TabNavigationInfo', function(){
+ .provider('TabNavigationInfo', function(analytics){
this.collapsed = [false, true]; // must match $scope.tables of TablesController
this.$get = function() {
var collapsed = this.collapsed;
diff --git a/app/scripts/controllers/tutorial.js b/app/scripts/controllers/tutorial.js
index 1700b71..68592a1 100644
--- a/app/scripts/controllers/tutorial.js
+++ b/app/scripts/controllers/tutorial.js
@@ -1,7 +1,7 @@
'use strict';
angular.module('tutorial', ['sql'])
- .controller('TutorialController', function ($scope, $location, $log, $timeout, $routeParams, SQLQuery) {
+ .controller('TutorialController', function ($scope, $location, $log, $timeout, $routeParams, SQLQuery, analytics) {
var base_url = "https://twitter.crate.io/api/v1";
var redirect = [window.location.protocol,window.location.host].join("//") + window.location.pathname;
diff --git a/app/scripts/services/analyticsService.js b/app/scripts/services/analyticsService.js
new file mode 100644
index 0000000..18e826b
--- /dev/null
+++ b/app/scripts/services/analyticsService.js
@@ -0,0 +1,34 @@
+var _gaq = _gaq || [];
+
+angular.module('analytics', []).run(['$http', function($http) {
+
+ _gaq.push(['_setAccount', 'UA-46278040-1']);
+ _gaq.push(['_trackPageview']);
+
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0];
+ s.parentNode.insertBefore(ga, s);
+
+}]).service('analytics', function($rootScope, $window, $location, $routeParams) {
+
+ $rootScope.$on('$viewContentLoaded', track);
+
+ var track = function() {
+ var path = convertPathToQueryString($location.path(), $routeParams)
+ $window._gaq.push(['_trackPageview', path]);
+ };
+
+ var convertPathToQueryString = function(path, $routeParams) {
+ for (var key in $routeParams) {
+ var queryParam = '/' + $routeParams[key];
+ path = path.replace(queryParam, '');
+ }
+
+ var querystring = decodeURIComponent($.param($routeParams));
+
+ if (querystring === '') return path;
+
+ return path + "?" + querystring;
+ };
+});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment