Skip to content
Docs menuInstall CentPath › Install the script

Add CentPath to Django

On this page

Add CentPath to the base template your Django templates extend, so every page that extends it counts its visits.

1. Copy your snippet

In CentPath, open your site, go to Setup → Install and click Copy. The snippet you copy holds your site's own id, so every visit counts for the right site:

<script>window.analytics = window.analytics || Object.assign([], { q: [], goal: function () { this.q.push(arguments); } });</script>
<script defer src="https://centpath.app/script/p.js" data-site-id="YOUR_SITE_ID"></script>

2. Paste it into your base template's head

  1. Open the template your other templates extend with {% extends %}, often named base.html: the one that holds <head>.
  2. Paste the snippet inside <head>, before </head>, outside any {% block %}. A child template that fills a block replaces what the block held, so outside every block the snippet stays on every page.
  3. Keep the two lines together, the first above the second. The first line keeps any goal your page sends before CentPath's script has loaded, and the script sends it once it runs. Set up goals shows how to send one.

Django's template language leaves the snippet as it is, since it holds no template variable or tag. If some of your templates don't extend the base template, add the snippet to their own <head> too.

3. Deploy your app

Deploy your app the way you always do, so your live pages carry the snippet.

Verify the setup

  1. Open your live site in a new tab and load any page.
  2. Back in CentPath, look under the steps in Setup → Install. Waiting for your first pageview… turns into First pageview received. once the visit arrives.
  3. If it said First pageview received. before you started, your site has had visits already: look for this one in your dashboard instead.
  4. Open your dashboard, or reload it: the visit is in Visitors and in the Visits feed, usually within a minute.

Visits from localhost count too, whenever your development server's pages load the snippet.

Troubleshooting

Setup still waits for the first pageview

  1. Load a page of your live site, not your development server.
  2. Open that page's source in your browser and search for your site's id. If it isn't there, that page's template doesn't extend the base template, or a block it fills held the snippet: move the snippet outside every block, or add it to that template.
  3. If your site sends a Content-Security-Policy, allow https://centpath.app in its script-src and connect-src, and let the snippet's first line run (a nonce or a hash in script-src).

For ad blockers, a wrong site id and other causes, see Troubleshooting.