Guest User

Untitled

a guest
Mar 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. function checkIfAnalyticsLoaded() {
  2. if (window._gaq && window._gaq._getTracker) {
  3. // Do tracking with new-style analytics
  4. } else if (window.urchinTracker) {
  5. // Do tracking with old-style analytics
  6. } else {
  7. // Retry. Probably want to cap the total number of times you call this.
  8. setTimeout(500, checkIfAnalyticsLoaded());
  9. }
  10. }
  11.  
  12. function checkIfAnalyticsLoaded() {
  13. if (window._gat && window._gat._getTracker) {
  14. // Do tracking with new-style analytics
  15. } else if (window.urchinTracker) {
  16. // Do tracking with old-style analytics
  17. } else {
  18. // Probably want to cap the total number of times you call this.
  19. setTimeout(checkIfAnalyticsLoaded, 500);
  20. }
  21. }
  22.  
  23. function check_ga() {
  24. if (typeof ga === 'function') {
  25. console.log('Loaded :'+ ga);
  26. } else {
  27. console.log('Not loaded');
  28. setTimeout(check_ga,500);
  29. }
  30. }
  31. check_ga();
  32.  
  33. ga(function(tracker) {
  34. console.log(tracker.get('clientId'));
  35. });
  36.  
  37. var ga = window[window['GoogleAnalyticsObject'] || 'ga'];
  38. if (typeof ga == 'function') {
  39. // call ga object here
  40. ga('send', 'event', 'Social Share Button', 'click', 'facebook');
  41. }
  42.  
  43. function add(a,b){alert(a + ' + ' + b + ' = ' + (a+b));}
  44. _mygaq(add,1,2);
  45.  
  46. function _mygaq(fn) {
  47. this._count = this._count || 0;
  48. this._running = this._running || false;
  49. this._q = this._q || [];
  50. if(arguments.length>0){
  51. this._q.push({"f":fn,"p":Array.prototype.slice.call(arguments,1)});
  52. } else {
  53. this._count++;
  54. }
  55. if ((window._gat && window._gat._getTracker) || window.urchinTracker) {
  56. this._count = 0;
  57. this._running = false;
  58. while (this._q.length > 0){
  59. var _innr = this._q[0];
  60. this._q = this._q.slice(1);
  61. _innr.f.apply(_innr.f, _innr.p);
  62. }
  63. } else {
  64. if( (arguments.length==0) || (!this._running && arguments.length>0)){
  65. if(this._count < 120) setTimeout('_mygaq()', 500);
  66. this._running = true;
  67. }
  68. }
  69. }
  70.  
  71. let checkIfAnalyticsLoaded = () => {
  72. return new Promise((resolve, reject) => {
  73. let timeStart = Date.now();
  74. const TIMEOUT = 3000;
  75.  
  76. let _isLoaded = function() {
  77. if (Date.now() - timeStart > TIMEOUT) {
  78. reject('Timeout. Google analytics not injected!');
  79. return;
  80. }
  81. if (window.ga && ga.create) {
  82. resolve(ga);
  83. return;
  84. } else {
  85. setTimeout(_isLoaded, 500);
  86. }
  87. };
  88.  
  89. _isLoaded();
  90. });
  91. }
  92.  
  93.  
  94. checkIfAnalyticsLoaded()
  95. .then((result => {
  96. console.log('LOADED', result);
  97. }))
  98. .catch(console.error);
  99.  
  100. ga.loaded
  101.  
  102. var t = setTimeout(function(){
  103. console.log(ga.loaded);
  104. }, 999);
  105.  
  106. <script type="text/javascript">
  107. var counter = 1;
  108. function checkIfAnalyticsLoaded() {
  109. if (window.ga) {
  110. //LOADED!
  111. } else {
  112. counter = counter + 1;
  113. if (counter < 6){
  114. setTimeout('checkIfAnalyticsLoaded()', 200);
  115. } else {
  116. //LOADED!
  117. }
  118. }
  119. }
  120. window.onload = checkIfAnalyticsLoaded();
  121. </script>
Add Comment
Please, Sign In to add comment