Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger google analytics event when form is valid in django

I have one form on my site, that other forms uses as base (and are on different urls) but they all do the same job, creating users.

I was thinking python/django could push a specific event to google analytics when the form is valid.

class FormBase(FormView):


    def form_valid(self, form): 


        #push to google analytics

        #_gaq.push(['_setAccount', 'UA-12345-1']);
        #_gaq.push(['_trackPageview', '/home/landingPage']);


        return super(FormBase, self).form_valid(form)

Is this possible? Would there be any caveats of doing it server side apposed to client side (javascript)

like image 867
Tomas Jacobsen Avatar asked Jan 16 '15 10:01

Tomas Jacobsen


1 Answers

Analytics tracking is usually done with Javascript on the page and form validation usually is a server side job. Your example kinda seems to mix Javascript and Python code that, of course, won't work.

Most commonly you'd just want to fire the event at the thank you page, using common Javascript. It's easy to do with Django, you just have to be careful that every time you use that form and it validates you send the user to the same thank you page, where you use a custom template that you built that has the event triggering code.

It is possible to do this server side using the measurement protocol. But there are a few things you have to be carefull:

1) Using the measurement protocol

The measurement protocol merely defines the HTTP request, there's no client library (official one at least), so you'd have to create and run the query yourself. This is going to add complexity to your project that is not really necessary.

2) Migrating to Universal Analytics

In your example you sem to use Classic Analytics. Google Analytics upgraded their tracking code to a new syntax called Universal Analytics. The measurement protocol only supports Universal Analytics, so if you are going to use the measurement protocol you should also upgrade all your site tracking code to Universal so the hits sent from client/server side are compatible.

3) Latency

The measurement protocol can take a few milliseconds to answer. If you are doing this millions of times, waiting for the HTTP response can become a bottleneck, delaying your routine. You probably want to create and issue this HTTP request in an asynchronous task using celery or something similar.

4) Session Continuation

Google Analytics uses a cookie on the client side to store the client id that is used to understand it's the same user across multiple requests. For that reason you want to issue a request from the server side using the same cid. This will allow GA to link the javascript hits with the other hits that user did on the website. So you need to grab the _ga cookie, parse it and extract the cid used in the request. Besides the cid you also want to extract the userIp and the userAgent from the real user to send in your request with the measurement protocol, GA uses these to create the Geo reports and Browser/OS reports.

As you can see it's a lot of work, maybe if you really have to track it server side it's worth it. But if you don't necessarily have to it's probably better to stick to the basics, and just use Javascript.

like image 151
Eduardo Avatar answered Sep 19 '22 15:09

Eduardo