1
votes

I have a mailing campaign where all links include google analytics tracking code such as:

http://example.com/account/somePage.html?utm_source=example&utm_medium=email&utm_campaign=reminder

The context /account/** is protected via Spring security and once the user clicks on the link on the email, he is re-directed to login BEFORE actually seeing somePage.html. This way the first page that is displayed is something like /login.do which does not have the analytics tracking code. Therefore google does not track my source, medium and campaign parameters.

Any ideas how to solve?

2
Why you want to protect URL pattern /account/** if you want anonymous access? - Michael
I don't want anonymous access. - checklist
With a default configuration, the user should be redirected back to the original URL after logging in, so the parameters should still be there when the page is rendered. - Shaun the Sheep
add the analytics code to login page - NimChimpsky
I do not have control over the login page (it is automatically redirected to via spring). - checklist

2 Answers

0
votes

Based on http://support.google.com/analytics/answer/1009614?hl=en , I updated my LoginController that shows the login page to redirect to /login?GOOGLE_PARAMATERS:

private static final String ALREADY_REDIRECTED = "ALREADY_REDIRECTED";
....

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView loginView(HttpServletRequest request, HttpServletResponse response){
....

Boolean alreadyRedirected = (Boolean) request.getSession().getAttribute(ALREADY_REDIRECTED);
    if (alreadyRedirected==null){
        SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
        if (savedRequest!=null){
            String source[] = savedRequest.getParameterValues("utm_source");
            if (source!=null && source.length>0){
                // we need to redirect with login instead

                String mediums[] = savedRequest.getParameterValues("utm_medium");
                String medium = mediums.length==0 ? "" : mediums[0];
                String campaigns[] = savedRequest.getParameterValues("utm_campaign");
                String campaign = campaigns.length==0 ? "" : campaigns[0];

                String redirect = "redirect:/login?utm_source=" + source[0] +  "&utm_medium=" + medium + "&utm_campaign=" + campaign;
                mav.setViewName(redirect);

                // mark not to do twice
                request.getSession().setAttribute(ALREADY_REDIRECTED, new Boolean(true));
                return mav;
            }
        }
    }
0
votes

We have similar problem and have solved with the next solution.

We have a signup form via Ajax, and in the callback if everything is OK we auto-login the user and lost Google Analytics tracking code for Funnel visualization because of Spring Security session invalidation and set up a new cookie.

What we have done by JS just before auto-login call the new user this

_gaq.push(['_trackPageview', '/signupDone']);

https://gist.github.com/moskinson/5418938

signupDone is a fake url that does not exists.

This way GA receive a call of a new url is loaded and we can track the funnel!

http://packageprogrammer.wordpress.com/2013/04/19/seguimiento-con-google-analytics-a-traves-del-login-con-spring-security/