Skip to content

Commit

Permalink
New code added. Old code removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Aug 5, 2010
1 parent dc7d921 commit 387c21d
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 405 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.pyc
build
*.swp
*~
*.egg-info
.*
dist
MANIFEST

9 changes: 0 additions & 9 deletions AUTHORS

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSE.mdown

This file was deleted.

3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include README.mdown

recursive-include googletools/templates *
134 changes: 15 additions & 119 deletions README.mdown
Original file line number Diff line number Diff line change
@@ -1,132 +1,28 @@
Google Analytics for Django Projects
====================================

I manage a lot of Django projects that present slightly-different forms to
users depending on the site/domain they're visiting. There's also a bunch of
custom submission code that differs from form to form, but that's neither here
nor there.
## Installation

I need different Google Analytics codes depending on the sites and after
sticking these tags into every single template, I thought it would be cool to
be able to manage these Google analytics accounts from the Django admin page.
I also added a mode of operation that excludes the admin interface altogether
(you can just use the template tag)
1. Add `googletools` to your `INSTALLED_APPS`
2. Run `./manage.py migrate` (or `./manage.py syncdb` if you do not make use of
South)


## Two modes of operation ##
## Management

### Administering and associating codes with Django `Sites` framework ###
Go to the admin interface. When correctly installed, you will find the
*Googletools* app. Here you can manage your Google Analytics and Site Verification
codes.

1. Add the `google_analytics` application to your `INSTALLED_APPS` section of your `settings.py`. This mode requires that you be using the Django sites framework too, so make sure you have that set up as well.
2. Add `GOOGLE_ANALYTICS_MODEL = True` to your `settings.py`
3. Run a `./manage.py syncdb` to add the database tables
4. Go to your project's admin page (usually `/admin/`) and click into a site objects
5. You'll now see a new field under the normal site information called "Analytics Code". In this box you put your unique analytics code for your project's domain. It looks like `UA-xxxxxx-x` and save the site.
6. In your base template (usually a `base.html`) insert this tag at the very top: `{% load analytics %}`
7. In the same template, insert the following code right before the closing body tag: `{% analytics %}`

### Just using the template tag ###
## Templatetags

### Google Analytics

1. Add the `google_analytics` application to your `INSTALLED_APPS` section of your `settings.py`.
2. In your base template, usually a `base.html`, insert this tag at the very top: `{% load analytics %}`
3. In the same template, insert the following code right before the closing body tag: `{% analytics "UA-xxxxxx-x" %}` the `UA-xxxxxx-x` is a unique Google Analytics code for you domain when you sign up for a new account.
Use `{% analytics_code %}` for inserting your Analytics code. This will return
an empty string when no code is setup for the current site.

### Google Site Verification

## Asynchronous Tracking ##

Google's recent asynchronous tracking API is also supported, as of v0.2. To use it,
simply call the templatetag `{% analytics_async %}` in the document head instead
of calling `{% analytics %}` just before the end of the body. You can also use
`{% analytics_async "UA-xxxxxx-x" %}` as with the old templatetags.

This is being added as an option rather than a replacement for two reasons:

1. Google recommends the Asynchronous Tracking snippet goes in the <head> tag, while
the old snippet goes at the end of the <body> tag, so as not to disrupt page loading.
Therefore it is not a drop in replacement in your template
2. The new snippet is reported to [break sites that have a comment before the head tag](http://www.stevesouders.com/blog/2009/12/01/google-analytics-goes-async/#comment-1171).
Adding the asynchronous tracking to existing code would cause backwards
incompatiblity.

## Supporting other tracking methods ##

Sometimes, the built-in code snippets are not sufficient for what you want to
do with Google Analytics. You might need to use different access methods,
or to support more complex Google Analytics functionality. Fortunately, using
different code snippets is dead easy, and there are two ways to do it.


### Overriding the analytics template ###

The easiest way is to override the `'google_analytics/analytics_template.html'`
template in a template directory that gets loaded before the one in the
`google_analytics` app.


### Registering a new analytics tag ###

You may want to keep the existing snippets around, while adding a new method.
Perhaps some of your pages need one snippet, but other pages need a different
one. In this case all you have to do is register a new tag in your tag
library using `do_get_analytics` like so:

from django import template
from google_analytics.templatetags import analytics

register = template.Library()
register.tag('my_analytics', analytics.do_get_analytics)

Then create a template at `'google_analytics/%(tag_name)s_template.html'`.
In this case the template name would be
`'google_analytics/my_analytics_template.html'`. Pass the variable
`{{ analytics_code }}` to the template wherever you need it.

The new tag will have all the same properties as the default tag, supporting
site-based analytics codes, as well as explicitly defined codes.

The best way to do this is to create a tiny app just for this purpose, so
you don't have to modify the code in `google_analytics`. Just put the above
code in `[app_name]/templatetags/[tag_library_name].py`. Then put your
template in `[app_name]/templates/google_analytics/[template_name]`. If your
app is named `my_analytics_app`, your tag library is named `more_analytics`,
and your tag is registered as `my_analytics`, the resulting app will have a
directory structure like this:

my_analytics_app/
+-- templatetags/
| +-- __init__.py
| \-- more_analytics.py
\-- templates/
\-- google_analytics/
\-- my_analytics_template.html
Finally, add `'my_analytics_app'` to `INSTALLED_APPS` in your `settings.py` file. Your new tag is
ready to go. To use the tag, put `{% load more_analytics %}` at the head of
your template. You can now access the `{% my_analytics %}` tag the same way
you would use `{% analytics %}`.


## License ##

The MIT License

Copyright (c) 2009 Clint Ecker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Use `{% site_verification_code %}` for inserting your site verification code.
This will return an empty string when no code is setup for the current site.
4 changes: 0 additions & 4 deletions google_analytics/admin.py

This file was deleted.

18 changes: 0 additions & 18 deletions google_analytics/fixtures/analytics_test.json

This file was deleted.

13 changes: 0 additions & 13 deletions google_analytics/models.py

This file was deleted.

This file was deleted.

This file was deleted.

62 changes: 0 additions & 62 deletions google_analytics/templatetags/analytics.py

This file was deleted.

1 change: 0 additions & 1 deletion google_analytics/tests/__init__.py

This file was deleted.

Loading

0 comments on commit 387c21d

Please sign in to comment.