Advertisement
Guest User

next

a guest
Oct 30th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. (function() {
  2. 'use strict';
  3.  
  4. var mod,
  5. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  6. __hasProp = {}.hasOwnProperty;
  7.  
  8. mod = angular.module('piwik', []);
  9.  
  10. mod.factory('PiwikActionMethods', function() {
  11. return ['setTrackerUrl', 'setSiteId', 'setCustomData', 'setCustomVariable', 'deleteCustomVariable', 'setLinkTrackingTimer', 'setDownloadExtensions', 'addDownloadExtensions', 'setDomains', 'setIgnoreClasses', 'setRequestMethod', 'setReferrerUrl', 'setCustomUrl', 'setDocumentTitle', 'setDownloadClasses', 'setLinkClasses', 'setCampaignNameKey', 'setCampaignKeywordKey', 'discardHashTag', 'setCookieNamePrefix', 'setCookieDomain', 'setCookiePath', 'setVisitorCookieTimeout', 'setSessionCookieTimeout', 'setReferralCookieTimeout', 'setConversionAttributionFirstReferrer', 'setDoNotTrack', 'addListener', 'enableLinkTracking', 'setHeartBeatTimer', 'killFrame', 'redirectFile', 'setCountPreRendered', 'trackGoal', 'trackLink', 'trackPageView', 'setEcommerceView', 'addEcommerceItem', 'trackEcommerceOrder', 'trackEcommerceCartUpdate'];
  12. });
  13.  
  14. mod.factory('PiwikGetMethods', function() {
  15. return ['getVisitorId', 'getVisitorInfo', 'getAttributionInfo', 'getAttributionCampaignName', 'getAttributionCampaignKeyword', 'getAttributionReferrerTimestamp', 'getAttributionReferrerUrl', 'getCustomData', 'getCustomVariable'];
  16. });
  17.  
  18. mod.factory('Piwik', [
  19. '$q', '$window', 'PiwikActionMethods', 'PiwikGetMethods', function($q, $window, PiwikActionMethods, PiwikGetMethods) {
  20. var Piwik;
  21. $window['_paq'] = $window['_paq'] || [];
  22. return Piwik = (function() {
  23. var method, _fn, _fn1, _i, _j, _len, _len1, _self;
  24.  
  25. function Piwik() {}
  26.  
  27. _self = Piwik;
  28.  
  29. _fn = function(method) {
  30. return _self[method] = function() {
  31. var arg, cmd, _j, _len1;
  32. cmd = [method];
  33. for (_j = 0, _len1 = arguments.length; _j < _len1; _j++) {
  34. arg = arguments[_j];
  35. cmd.push(arg);
  36. }
  37. return $window['_paq'].push(cmd);
  38. };
  39. };
  40. for (_i = 0, _len = PiwikActionMethods.length; _i < _len; _i++) {
  41. method = PiwikActionMethods[_i];
  42. _fn(method);
  43. }
  44.  
  45. _fn1 = function(method) {
  46. return _self[method] = function() {
  47. var deferred, _args;
  48. deferred = $q.defer();
  49. _args = arguments;
  50. $window['_paq'].push(function() {
  51. try {
  52. return deferred.resolve(this[method].apply(this, _args));
  53. } catch (e) {
  54. return deferred.reject(e);
  55. }
  56. });
  57. return deferred.promise;
  58. };
  59. };
  60. for (_j = 0, _len1 = PiwikGetMethods.length; _j < _len1; _j++) {
  61. method = PiwikGetMethods[_j];
  62. _fn1(method);
  63. }
  64.  
  65. return Piwik;
  66.  
  67. })();
  68. }
  69. ]);
  70.  
  71. mod.directive('ngpPiwik', [
  72. '$window', '$document', 'Piwik', 'PiwikActionMethods', function($window, $document, Piwik, PiwikActionMethods) {
  73. var arr_param_methods, build_p_call, comma_regex, dir_def_obj;
  74. $window['_paq'] = $window['_paq'] || [];
  75. arr_param_methods = ['setDomains', 'setDownloadClasses', 'setIgnoreClasses', 'setLinkClasses'];
  76. comma_regex = /,/g;
  77. comma_regex.compile(comma_regex);
  78. build_p_call = function(method, attr_val) {
  79. var call, param, _i, _len, _ref;
  80. call = [method];
  81. if ((__indexOf.call(arr_param_methods, method) >= 0) && comma_regex.test(attr_val)) {
  82. call.push(attr_val.split(','));
  83. } else {
  84. _ref = attr_val.split(',');
  85. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  86. param = _ref[_i];
  87. call.push(param);
  88. }
  89. }
  90. return call;
  91. };
  92. dir_def_obj = {
  93. restrict: 'E',
  94. replace: false,
  95. transclude: true,
  96. compile: function(tElement, tAttrs, transclude) {
  97. var script_elem;
  98. script_elem = $document[0].createElement('script');
  99. script_elem.setAttribute('src', tAttrs.ngpSetJsUrl);
  100. $document[0].body.appendChild(script_elem);
  101. return {
  102. post: function(scope, elem, attrs) {
  103. var k, v;
  104. for (k in attrs) {
  105. if (!__hasProp.call(attrs, k)) continue;
  106. v = attrs[k];
  107. if (/^ngp/.test(k)) {
  108. (function(k, v) {
  109. var method;
  110. method = k[3].toLowerCase() + k.slice(4);
  111. if (!(__indexOf.call(PiwikActionMethods, method) >= 0)) {
  112. return;
  113. }
  114. $window['_paq'].push(build_p_call(method, v));
  115. return attrs.$observe(k, function(val) {
  116. return $window['_paq'].push(build_p_call(method, val));
  117. });
  118. })(k, v);
  119. }
  120. }
  121. return $window['_paq'].push(['trackPageView']);
  122. }
  123. };
  124. }
  125. };
  126. return dir_def_obj;
  127. }
  128. ]);
  129.  
  130. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement