Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7515863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:56:54+00:00 2026-05-30T00:56:54+00:00

I want track offline event using Google Analytic and Local Storage. this is my

  • 0

I want track offline event using Google Analytic and Local Storage.
this is my code:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-27966345-1']);
    _gaq.push(['_setDomainName', 'none']);
    _gaq.push(['_setSessionCookieTimeout',10]);
    _gaq.push(['_setSampleRate', '400']);
    _gaq.push(['_trackPageview']);
    (function() {
       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);
     })();

I simply save the events in local storage and when user get back online I try to send events to Google in a for but when I compare my counter with page-views that I see in Real Time mode(Google Analytic) I can’t understand why they are different. I think is about Google sample rate or something because I test it many times and I see different results, sometimes the results is correct but sometimes have 1000 or more difference.

Here is my code for sending events:

      while(ctr>0){
        if(sd==0){
            sd=1;
            alert(ctr);
        }
        //
        if(flag==0)break;
        var name='tosend';
        var tosend_action=localStorage.getItem(name+'action'+ctr);
        var tosend_label=localStorage.getItem(name+'label'+ctr);
        var tosend_value=localStorage.getItem(name+'value'+ctr);
        _gaq.push(['_trackEvent',value,tosend_action,tosend_label+"_val:"+tosend_value,tosend_value]);
        _gaq.push(['_trackPageview',name+'value'+ctr]);
        localStorage.removeItem(name+'action'+ctr);
        localStorage.removeItem(name+'label'+ctr);
        localStorage.removeItem(name+'value'+ctr);
        ctr=Number(ctr)-1;
        localStorage.removeItem('counter');
        localStorage.setItem('counter',ctr);
        ctr=localStorage.getItem('counter');
       }

}

p.s: flag is the my variable to see if user is online or not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:56:56+00:00Added an answer on May 30, 2026 at 12:56 am

    Google will rate limit you on the consecutive events you can send. Here are the rules.

    • You can send up to 10 hits (Events or pageview) in a single burst.
    • After that all hits are silently droped.
    • each 5s you get 1 extra hit. Up to the maximum of 10.

    It’s like a Token Bucket Algorithm where the maximum tokens are 10 and the refresh rate is 1 new token every 5 seconds.

    Now _setSampleRate and _setSessionCookietimeout won’t help you here, you should remove these parameters from your tracking code. The best thing you can do is to throttle you’re requests, implementing the same algo on your end. Here’s an example on how you could do that:

    var tokens = 10;
    
    function update_tokens() {
        if (tokens < 10) tokens++;
    }
    
    // Even though new tokens should be generated each 5 seconds I give it 10 seconds just to make sure we have tokens available.
    setInterval(update_tokens, 10 * 1000);
    
    
    var hits_to_send = [
        ['_trackPageview', '/page1'],
        ['_trackPageview', '/page2'],
        ['_trackEvent', 'category', 'action', 'label'],
        //...
        ];
    
    // Recursive function to check tokens and send requests.
    function send_next() {
        if (hits_to_send.length==0) return;
        if (tokens > 0) {
            tokens--;
            _gaq.push(hits_to_send.shift());
        }
        else {
            setTimeout(send_next, 5 * 1000);
            return;
        }
        send_next();
        return;
    }
    
    
    //When you go online just call:
    send_next();
    

    ​

    This should give you better numbers even though some metrics won’t look nice. timeOnSite and timeOnPage for example. A new visit may be created if the user has been offline for more than 30 min even though he was interacting with the system.

    Also notice that if you have too many events in there it can take a while to update all of them. I’d recommend you to keep your events to a reasonable amount. Try to track only things that are important for you future analysis.

    GA Hit Limit Reference

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to track page views using Google Analytics in my iPhone application. I
I want to track the location (longitude and latitude) of the mobile using php
recently this post inspired me, I want to track my own life, too. I
I want to track my GPS device using gprs connection, which should give details
I want to learn to track a moving ball using Kalman filter. Although many
I want to track user actions on a page using jQuery. I track now
I want to track a JavaScript event on a page, but I don't want
Suppose i want to track the progress of a loop using the progress bar
For my Grails application I want to set up Google Analytics to track only
I want to track people (carrying mobile devices) in a pedestrian street by using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.