1

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
2
+50

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);
    bla.loginWithGoogle({
      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()) {
      this.render(this.loadingTemplate); 
    } else {
      console.log('Some crazy stuff');
      loginWithGoogle();
    }
  } else {
    this.next();
  }
}

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)

bla.startup(function(){
    $(window).bind('beforeunload', function() {
        closingWindow();
    });
});

closingWindow = function(){
    console.log('You are leaving.');
    bla.logout();
}
| improve this answer | |
1

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

| improve this answer | |
1

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.

| improve this answer | |

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.