8

I would like to track a login event on my website. The user writes username and pass then clicks on login, the form is submited and server checks if password is correct then redirect to the home page if it is. But how could I add a Login event to GA? If I add it to the login button it wont be totally accurate as it will count even the failed login attempts.

Any ideas on how to solve this?

Thanks Chris

Chris Hki
  • 137
  • 1
  • 1
  • 6

5 Answers5

7

Great question!

I think what you want is the Custom Variables that google analytics offers.

Simply put, for each page your user visits you set a custom variable with it's username for example.

I don't think you are interested in the login event, rather you are interested in what a logged in user visits - and this solution solves your problem

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • 2
    I use custom vars exactly for this. This article breaks it down a bit better than Google docs: [Quick Tip: The Power of Google Analytics Custom Variables](http://net.tutsplus.com/tutorials/other/quick-tip-the-power-of-google-analytics-custom-variables/) – jk. Dec 22 '11 at 23:19
  • 2
    storing the username in a Google Analytics custom variable is a violation of their TOS. See http://stackoverflow.com/questions/10095225/tracking-individual-users-with-google-analytics-custom-variables – zacropetricopus Feb 21 '14 at 03:00
  • Now we can set up the User ID in the tracking code. See: https://support.google.com/analytics/answer/3123666 – ruhong Sep 15 '16 at 11:17
  • 1
    Despite being accepted, this goes against original intent of **capturing login events** which, contrary to this answer's assumptions, some of us **actually do** want to record.. @joesk's answer is more applicable. – kevlarr Nov 30 '17 at 22:19
5

The Custom Variables answer will serve your purposes as outlined below but if you want another alternative (or actually really want to use Track Events) you could also add in a parameter to a successful logon which you can then read and process as you wish.

So for example:

 <a href="<?php echo wp_login_url(get_permalink().'?login=true');  ?>">Login</a>

This will create a link to your login page. If the login is successful it will redirect back to the current page with the parameter login=true in the URL.

(You could check this parameter via JS for example and fire the Analytics track event call based on this).

joesk
  • 174
  • 5
  • 1
    This is a MUCH better answer than the accepted. Contrary to what the accepted answer assumes (which actually goes against the original question), some of us **DO** want to track actual login events. Check for `login=true` on client side, send an event if present, and then remove from URL so that a reload can't send an extraneous event. – kevlarr Nov 30 '17 at 22:16
  • Note that one should use `replaceState` so that there isn't a history object they can go back to that has the `login=true` query param... assuming that IE9 and below does not need to be supported. – kevlarr Nov 30 '17 at 22:29
0

Custom Vars can be used for individual users but you need to set up a unique ID so that only you could recognise that once you pull the data out of GA. So in your dbase set a GoogleAnalyticsID against each user, then send that as a custom var to track users.

VineFreeman
  • 715
  • 7
  • 12
0

One way to do this is let your login redirect to a page which says something like: "Thank you for logging in" and register this pageview to Google Analytics. And then have that page auto redirect you after 5 seconds to the page you were viewing. I've seen this done on a good amount of websites. If your login is using partial refresh you could even do it without having the user pass by a seperate page.

IvanL
  • 2,475
  • 1
  • 26
  • 39
0

The simplest way is to use virtual pageviews (tutorial). It's a small piece of JS code, that you execute on any event you want. It makes GA think that there was a pageview. So you make a conditional statement like "if login == OK -> create a virtual pageview with URI "virtual/login/OK". Then you simply set this URI as a goal.

Nikolay
  • 2,206
  • 3
  • 20
  • 25