Have a question?

Django SDK

Track customers directly in the Django Web Framework using Woopra’s Django SDK

Download the SDK

Download Django SDK from GitHub

Django SDK Documentation

This SDK allows our users who have servers running the Django Framework to track their visitors via Python code. Tracking directly in Python allows both front-end and back-end visitor tracking:

  • Front-end: after configuring the tracker, identifying the user, and tracking events in Python, the SDK will generate the corresponding JavaScript code. By passing this code to your view using a hook (example below), you will be able to print that code in your view’s header.
  • Back-end: after configuring the tracker and identifying the user, add the optional parameter: true to the methods track or push. The Python tracker will send the data to Woopra by making HTTP Requests. In this case, the client is never involved in the tracking process.

The first step is to setup the tracker SDK in your views.py file. For example, if you want to set up tracking with Woopra on your homepage, the file views.py should look like:

# import the SDK
import woopra_tracker

def homepage(request):
   woopra = woopra_tracker.WoopraTracker(request)
   woopra.config({'domain' : 'mybusiness.com'})

   # Your code here...

You can also customize any of Woopra’s properties during this step by adding them to the config_dict.

For example, to update your idle timeout (default: 30 seconds):

# instead of:
woopra.config({'domain' : 'mybusiness.com'})
# directly write:
woopra.config({'domain' : 'mybusiness.com', 'idle_timeout' : 15000})

To add custom visitor properties, use the identify(user_dict) function:

woopra.identify({'email' : 'johndoe@mybusiness.com', 'name' : 'John Doe', 'company' : 'My Business'})

To identify a user without tracking, you can push() the update to Woopra:

woopra.identify(user).push()
# or, to push through back-end:
woopra.identify(user).push(True)

To track page views, simply call track():

woopra.track()
# For back-end tracking:
woopra.track(back_end_tracking = True)

You can also track custom events through front-end or back-end tracking. Combining previous steps, your views.py file should look like:

import woopraTracker

def homepage(request):
   woopra = woopra_tracker.WoopraTracker(request)
   woopra.config(config_dict).identify(user_dict).track()
   # Track a custom event through the front end...
   woopra.track("play", {'artist' : 'Dave Brubeck', 'song' : 'Take Five', 'genre' : 'Jazz'})
   # ... and through the back end by passing the optional param True
   woopra.track('signup', {'company' : 'My Business', 'username' : 'johndoe', 'plan' : 'Gold'}, True)

   # Your code here...
   
   # When you're done setting up your WoopraTracker object, send a hook containing the value of
   # woopra.js_code() among all the other hooks you are passing to the template.
   response = render(request, 'homepage.html', {'woopra_code': woopra.js_code(), 'foo' : 'bar', })
   return response

add the hook in your template’s header (here homepage.html)

<!DOCTYPE html>
<html>
   <head>
      <!-- Your header here... -->
      <!-- Make sure to deactivate html auto-escaping -->
      {% autoescape off %}{{ woopra_code }}{% endautoescape %}
   </head>
   <body>
      <!-- Your body here... -->

   </body>
</html>

Finally, if you wish to track your users only through back-end tracking, you should set the cookie on your user’s browser. If you are planning to do front-end tracking in addition, you can skip this step as the JavaScript tracker will handle it for you.

...
response = render(request, 'homepage.html', {'woopra_code': woopra.js_code(), 'foo' : 'bar', })
#Set the cookie after the response was rendered, and before sending any headers:
woopra.set_woopra_cookie(response)
return response

Need any help? Contact our Data Jedis