Is there any possibility using the Google Analytics API to check if the session is logged in or not? I have a problem with destination page (it is not set), and I want to resend the event when the session is not logged in.

  • 2
    I'm not sure what you mean by "using the Google Analytics API to check if the session is logged or not". Are you referring to modifying the tracking script to handle exceptions? Please elaborate on the issue and what you're trying to accomplish.
    – nburr
    Nov 11 '15 at 20:49

You can use something like this

1) Set up onBeforeAction hooks to login the user automatically (which asks for credentials if user is not logged in to external service)

var loginWithGoogle = function() {
  if (bla.isClient) {
    Session.set('loginError', undefined);
      loginStyle         : "redirect",
      requestPermissions : ['profile', 'email'],
      requestOfflineToken: true
    }, function (err) {
      if (err)
        Session.set('loginError', 'reason: ' + err.reason + ' message: ' + err.message || 'Unknown error');

var requireLogin = function() {
  if (! bla.user()) {
    if (bla.loggingIn()) {
    } else {
      console.log('Some crazy stuff');
  } else {

Router.onBeforeAction(requireLogin, {except: ['some-special-public-route']});

2) Log the user out when they are navigating away from every page (caveat: login/logout gets called everytime the user navigates within the app)

    $(window).bind('beforeunload', function() {

closingWindow = function(){
    console.log('You are leaving.');

You can force to start a new session in case You need it using the sessionControl. Send a hit like :

ga(‘send’, ‘pageview’, {‘sessionControl’: ‘start’}); 

Further info https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#sessionControl


There should be a request to _utm.gif? from the page. (See this blog post and this SO answer.)

Unfortunately I can't find out if/how it's possible to know if a request has been done from a page. This code is the best I quickly found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.