/*
* getUserEmail()
* Figure out the user's correct email address. This helps the differing keys
* in the database when using oAuth login.
*/
getUserEmail = function(){
  if ( Meteor.user().emails ) {
    if (Meteor.user().emails[0]) {
      return Meteor.user().emails[0].address;
    } else {
      return null;
    }
  } else if ( Meteor.user().services ) {
    var services = Meteor.user().services;
    if ( services.facebook ) {
      return services.facebook.email;
    } else if ( services.github ) {
      return services.github.email;
    } else if ( services.google ) {
      return services.google.email;
    } else {
      return null;
    }
  }
}

trackLogins = function () {
  if (Meteor.user) {
    if (Meteor.userId()) {
      var userId = Meteor.userId();
      console.log(">> new userId: ", userId);
      var userEmail;
      Tracker.nonreactive(function (){
        userEmail = getUserEmail();
      })
      analytics.identify(userId, {email: userEmail});
      analytics.track("Signed in");
    }

    if (!Meteor.userId()) {
      console.log(">> user logged out");
      analytics.track("Signed out");
    }
  }
};

if (Package['iron:router']) {
  console.log("package iron:router found!!");
  Package['iron:router'].Router.onRun(function() {
    console.log("hit onRun!!");
    analytics.page(this.route.getName());
    this.next();
  });
}

Meteor.startup(function () {
  Tracker.autorun(trackLogins);
});

@@ -162,16 +162,41 @@
 tion()%7B%0A
+  if (Meteor.user()) %7B%0A  
   if ( M
@@ -215,32 +215,34 @@
 .emails ) %7B%0A    
+  
 if (Meteor.user(
@@ -252,24 +252,26 @@
 mails%5B0%5D) %7B%0A
+  
       return
@@ -304,24 +304,26 @@
 ddress;%0A    
+  
 %7D else %7B%0A   
@@ -311,32 +311,34 @@
 %0A      %7D else %7B%0A
+  
       return nul
@@ -344,18 +344,22 @@
 ll;%0A    
-%7D%0A
+  %7D%0A  
   %7D else
@@ -387,24 +387,26 @@
 ervices ) %7B%0A
+  
     var serv
@@ -436,24 +436,26 @@
 rvices;%0A    
+  
 if ( service
@@ -465,24 +465,26 @@
 acebook ) %7B%0A
+  
       return
@@ -505,32 +505,34 @@
 book.email;%0A    
+  
 %7D else if ( serv
@@ -545,32 +545,34 @@
 ithub ) %7B%0A      
+  
 return services.
@@ -577,32 +577,34 @@
 s.github.email;%0A
+  
     %7D else if ( 
@@ -625,24 +625,26 @@
 e ) %7B%0A      
+  
 return servi
@@ -661,24 +661,26 @@
 .email;%0A    
+  
 %7D else %7B%0A   
@@ -672,24 +672,26 @@
    %7D else %7B%0A
+  
       return
@@ -693,24 +693,32 @@
 eturn null;%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A
@@ -718,16 +718,20 @@
 %0A  %7D%0A%7D%0A%0A
+var 
 trackLog
@@ -767,24 +767,28 @@
 eor.user
+Id()
 ) %7B%0A    
 if (Mete
@@ -783,50 +783,50 @@
     
-if (Meteor.userId()) %7B%0A      var userId = 
+analytics.track(%22Signed in%22);%0A  %7D%0A%0A  if (!
 Mete
@@ -836,20 +836,20 @@
 userId()
-;%0A  
+) %7B%0A
     cons
@@ -864,79 +864,85 @@
 %22%3E%3E 
-new userId: %22, userId);%0A      var userEmail;%0A      Tracker.nonreactive(
+user logged out%22);%0A    analytics.track(%22Signed out%22);%0A  %7D%0A%7D;%0A%0Avar identify = 
 func
@@ -948,25 +948,51 @@
 ction ()
+ 
 %7B%0A  
- 
+if (Meteor.userId()) %7B%0A
     
+var
  userEma
@@ -1020,54 +1020,12 @@
     
-  %7D)%0A      analytics.ident
 if
-y(userId, %7Bemail: 
+ (
 user
@@ -1029,19 +1029,19 @@
 serEmail
-%7D);
+) %7B
 %0A      a
@@ -1053,44 +1053,17 @@
 ics.
-track(%22Signed in%22);%0A    %7D%0A%0A    if (!
+identify(
 Mete
@@ -1077,87 +1077,28 @@
 Id()
-) %7B%0A      console.log(%22%3E%3E user logged out%22);%0A      analytics.track(%22Signed out%22
+, %7Bemail: userEmail%7D
 );%0A 
@@ -1398,12 +1398,41 @@
 ogins);%0A
+  Tracker.autorun(identify);%0A
 %7D);%0A

Split identify and track "Sign in" event steps