Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkIfAnalyticsLoaded() {
- if (window._gaq && window._gaq._getTracker) {
- // Do tracking with new-style analytics
- } else if (window.urchinTracker) {
- // Do tracking with old-style analytics
- } else {
- // Retry. Probably want to cap the total number of times you call this.
- setTimeout(500, checkIfAnalyticsLoaded());
- }
- }
- function checkIfAnalyticsLoaded() {
- if (window._gat && window._gat._getTracker) {
- // Do tracking with new-style analytics
- } else if (window.urchinTracker) {
- // Do tracking with old-style analytics
- } else {
- // Probably want to cap the total number of times you call this.
- setTimeout(checkIfAnalyticsLoaded, 500);
- }
- }
- function check_ga() {
- if (typeof ga === 'function') {
- console.log('Loaded :'+ ga);
- } else {
- console.log('Not loaded');
- setTimeout(check_ga,500);
- }
- }
- check_ga();
- ga(function(tracker) {
- console.log(tracker.get('clientId'));
- });
- var ga = window[window['GoogleAnalyticsObject'] || 'ga'];
- if (typeof ga == 'function') {
- // call ga object here
- ga('send', 'event', 'Social Share Button', 'click', 'facebook');
- }
- function add(a,b){alert(a + ' + ' + b + ' = ' + (a+b));}
- _mygaq(add,1,2);
- function _mygaq(fn) {
- this._count = this._count || 0;
- this._running = this._running || false;
- this._q = this._q || [];
- if(arguments.length>0){
- this._q.push({"f":fn,"p":Array.prototype.slice.call(arguments,1)});
- } else {
- this._count++;
- }
- if ((window._gat && window._gat._getTracker) || window.urchinTracker) {
- this._count = 0;
- this._running = false;
- while (this._q.length > 0){
- var _innr = this._q[0];
- this._q = this._q.slice(1);
- _innr.f.apply(_innr.f, _innr.p);
- }
- } else {
- if( (arguments.length==0) || (!this._running && arguments.length>0)){
- if(this._count < 120) setTimeout('_mygaq()', 500);
- this._running = true;
- }
- }
- }
- let checkIfAnalyticsLoaded = () => {
- return new Promise((resolve, reject) => {
- let timeStart = Date.now();
- const TIMEOUT = 3000;
- let _isLoaded = function() {
- if (Date.now() - timeStart > TIMEOUT) {
- reject('Timeout. Google analytics not injected!');
- return;
- }
- if (window.ga && ga.create) {
- resolve(ga);
- return;
- } else {
- setTimeout(_isLoaded, 500);
- }
- };
- _isLoaded();
- });
- }
- checkIfAnalyticsLoaded()
- .then((result => {
- console.log('LOADED', result);
- }))
- .catch(console.error);
- ga.loaded
- var t = setTimeout(function(){
- console.log(ga.loaded);
- }, 999);
- <script type="text/javascript">
- var counter = 1;
- function checkIfAnalyticsLoaded() {
- if (window.ga) {
- //LOADED!
- } else {
- counter = counter + 1;
- if (counter < 6){
- setTimeout('checkIfAnalyticsLoaded()', 200);
- } else {
- //LOADED!
- }
- }
- }
- window.onload = checkIfAnalyticsLoaded();
- </script>
Add Comment
Please, Sign In to add comment