diff --git a/src/page/home/home.html b/src/page/home/home.html index 6783e8f..ae54470 100644 --- a/src/page/home/home.html +++ b/src/page/home/home.html @@ -6,7 +6,7 @@

Hello, Angular!

Welcome to your Angular.js app!

-

Learn more

@@ -40,4 +40,4 @@

Hello, Angular!

- \ No newline at end of file + diff --git a/src/service/index.js b/src/service/index.js index 9890153..c98a52f 100644 --- a/src/service/index.js +++ b/src/service/index.js @@ -2,3 +2,4 @@ require('./util.js'); require('./store.js'); require('./api.js'); require('./httpInterceptor.js'); +require('./tj.js'); diff --git a/src/service/tj.js b/src/service/tj.js new file mode 100644 index 0000000..907a8d3 --- /dev/null +++ b/src/service/tj.js @@ -0,0 +1,84 @@ +var app = require('app'); +// TJ大神,佑我无BUG +app.factory('TJ', function($location, $rootScope, $resource) { + return { + formUrl: document.referrer, // $locationChangeStart + init: function(option) { + var self = this; + + option = angular.extend({ + url: $location.absUrl(), + }, option); + + this.pageInit().get(option, function(data) { + console.log(data, 'success'); + }, function(data) { + console.log(data, 'error'); + }); + + this.formUrl = option.url; + + $rootScope.$on('$locationChangeStart', function() { + // 统计流向 + self.save({ from: self.formUrl, target: $location.absUrl() }); + }); + }, + save: function(option, isTJPage) { + var formUrl = this.formUrl; + + option = angular.extend({ + from: formUrl, + //target: '', + }, option); + + // 初始化或刷新页面都不需要统计为跳转事件 + if (!option.from || option.from === option.target) { + return false; + } + + this.openPage().get(option, function(data) { + console.log(data, 'success'); + }, function(data) { + console.log(data, 'error'); + }); + + this.formUrl = isTJPage ? option.from : option.target; + }, + // api pageInit + pageInit: function() { + return $resource('/bi/pageInit'); + }, + // api openPage + openPage: function() { + return $resource('/bi/openPage'); + }, + }; +}); + +// 指定需要统计的链接 +app.directive('tjPage', function() { + return { + restrict: 'AE', + controller: function() { + this.isTJPage = true; + } + }; +}).directive('a', function(TJ) { + return { + restrict: 'AE', + require: '^?tjPage', + replace: true, + link: function(scope, element, attrs, tjPageController) { + tjPageController && tjPageController.isTJPage && + element.on('click', function(e) { + // 统计流向 + TJ.save({ from: TJ.formUrl, target: e.target.getAttribute('href') }, true); + }); + }, + }; +}); + +app.run(function(TJ) { + // 统计初始化页面 + TJ.init(); +});