Tracking codes

### Navigation

- index
- next |
- previous |
- fantastico 0.4.0-b114 documentation »
- Fantastico features »
- Built in components »

# Tracking codes

Every web application usually requires support for tracking visitors behavior. Most of the solutions can be easily integrated into a website by adding a small javascript snippet into every page you want to track. Below you can find some popular tracking solutions (free or commercial).

## Analytic solutions

At the moment of writting this article, there are plenty of options available for web developers to track their website performance:

> 1. Google analytics [https://www.google.com/analytics/] (probably the most popular solution).
> 2. Reinvigorate [https://www.reinvigorate.net/].
> 3. KISSmetrics [https://www.kissmetrics.com/].
> 4. FoxMetrics [http://foxmetrics.com/].
> 5. Mint [http://haveamint.com/].
> 6. Open Web Analytics [http://www.openwebanalytics.com/].
> 7. Clicky [http://clicky.com/].
> 8. Mixpanel [https://mixpanel.com/].
> 9. Chartbeat [https://chartbeat.com/].
> 10. Adobe Web Analytics [http://www.adobe.com/solutions/digital-analytics.html].
> 11. Chartbeat [https://chartbeat.com/].
> 12. Inspectlet [http://www.inspectlet.com/].

Of course there are many other solutions available out there. For more information about the above mentioned solution I recommend you read the excellent article [http://www.onextrapixel.com/2013/07/16/ten-best-alternatives-to-google-analytics/] posted by Aidan Huang.

## Integration

Follow the steps from this section in order to enable tracking in Fantastico projects:

1. Activate tracking extension:

       fsdk activate-extension --name tracking_codes --comp-root <comp_root>

2. Add your tracking codes into database (easiest way is through Syncdb command)

3. Create a sql script similar to the one below and place it under <comp_root>/sql/create_data.sql:

       INSERT INTO tracking_codes(provider, script)
       VALUES ('Google Analytics', '
          <script type="text/javascript">
               var _gaq = _gaq || [];
               _gaq.push(["_setAccount", "UA-XXXXX-X"]);
               _gaq.push(["_trackPageview"]);

               (function() {
                 var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
                 ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
                 var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
               })();
          </script>');

4. Update your project database

       fsdk syncdb --db-command /usr/bin/mysql --comp-root <comp_root>

5. Use tracking codes in your pages:

       {% component url="/tracking-codes/ui/codes/" %}{% endcomponent %}

Tracking component is rendering all available codes from the database. In order to check all available tracking codes configured in a Fantastico project visit http://localhost:12000/tracking-codes/ui/codes/. Once the page is loaded see page source.

## Current limitations

In the first version of this component (part of Fantastico 0.4) there are some known limitations:

> - No API provided for Create / Update / Delete operations.

## Technical summary

class `fantastico.contrib.tracking_codes.tracking_controller.` `TrackingController`(settings_facade)

  This class provides the tracking operations supported by TrackingCodes component.

  `list_codes`(*args, **kwargs)

    This method provides tracking codes listing logic. It list all available tracking codes from database.

    Parameters: | request (`webob.request.Request`) – The current http request being processed. Read Request lifecycle for more information.
    Returns: | JSON list of available tracking codes. Can be empty if no tracking codes are defined.

  `list_codes_ui`(*args, **kwargs)

    This method renders all available tracking codes.

class `fantastico.contrib.tracking_codes.models.codes.` `TrackingCode`(provider, script)

  This class provides the model for tracking codes. It maps tracking_codes table to an object. In order to use this model please read Model View Controller.