Skip to main content

Django app supporting Net Promoter Score (NPS) surveys.

Project description

# Django NPS

Django app supporting Net Promoter Score (NPS) surveys

## Compatibility

This package supports Django 4.2+, Python 3.10+.

## Background - Net Promoter Score

The NPS is a measure of customer loyalty that is captured by asking your customers a single
question:

> How likely is it that you would recommend our [company|product|service] to a friend or colleague?"

The answer to this question is a number from 0-10 (inclusive). These scores are then broken out into
three distinct groups: 'detractors' (0-6), 'neutral' (7-8) and 'promoters' (9-10). The NPS is then
the difference between the number of promoters and detractors (as a percentage of the whole
population).

For example, if you ask 100 people, and you get the following results:

```
detractors: 20%
neutrals: 10%
promoters: 70%
```

Then your NPS is 70 - 20 = 50. _(NPS is expressed as a number, not a %)_

NPS was originally developed at the strategy consultants Bain & Company by Fred Reichheld in 2003.
They retain the registered trademark for NPS, and you can read all about the history of it on their
site "[Net Promoter System](http://netpromotersystem.com/about/index.aspx)".

## Usage

This app is used to store the individual scores, and calculate the NPS based on these. It does not
contain any templates for displaying the question itself, neither does it put any restriction around
how often you ask the question, or to whom. It is up to the app developer to determine how this
should work - each score is timestamped and linked to a Django User object, so you can easily work
out the time elapsed since the last time they were asked.

For example, if you want to ensure that you only survey users every X days, you can add a context
property to the template using the `display_to_user` method:

```python
>>> # only show the survey every 90 days
>>> UserScore.objects.days_since_user_score(request.user) > 90
True
```

If you then show the survey - the output of which is a single value (the score) together with an
optional reason ("what is the main reason for your score"), is then posted to the `post_score`
endpoint, which registers the user score.

The NPS value itself can be calculated on any queryset of `UserScore` objects - which allows you to
track the score based on any attribute of the score itself or the underlying user. For instance, if
you have custom user profiles, you may wish to segment your NPS by characteristics of those
profiles.

```python
>>> # December's NPS
>>> UserScore.objects.filter(timestamp__month=12).net_promoter_score()
50
```

The `post_score` endpoint returns a `JsonResponse` which contains a `'success': True|False` value
together with the `UserScore` details:

```python
{
"success": True,
"score": {"id": 1, "user": 1, "score": 0, "group": "detractor"}
}
```

If the score was rejected, the errors are returned in place of the score (errors are a list of
lists, as returned from the Django `Form.errors` property):

```python
{
"success": False,
"errors": [["score", "Score must be between 0-10"]]
}
```

The app contains a piece of middleware, `NPSMiddleware`, which will add an attribute to the
`HttpRequest` object called `show_nps`. If you add the middleware to your settings:

```python
# settings.py
MIDDLEWARE_CLASSES = (
# standard django middleware
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'net_promoter_score.NPSMiddleware',
)
```

You can then use this value in your templates:

```html
<!-- show_nps template = {{request.show_nps}} -->
{% if request.show_nps %}
<div>HTML goes here</div>
{% endif %}
```

## Settings

**NPS_DISPLAY_INTERVAL**

The number of days between surveys, integer, defaults to 30. This value is used by the default
`show_nps` function to determine whether someone should be shown the survey.

**NPS_DISPLAY_FUNCTION**

A function that takes an `HttpRequest` object as its only argument, and which returns True if you
want to show the survey. This function is used by the `net_promoter_score.show_nps` function. It
defaults to return True if the request user has either never seen the survey, or hasn't seen it for
more days than the `NPS_DISPLAY_INTERVAL`.

This function should be overridden if you want fine-grained control over the process - it's the main
hook into the app.

## Tests

There is a full suite of tests for the app, which are best run through `tox`. If you wish to run the
tests outside of tox, you should install the requirements first:

```
$ pip install -r requirements.txt
$ python manage.py test
```

## License

MIT

## Contributing

Usual rules apply:

1. Fork to your own account
2. Create a branch, fix the issue / add the feature
3. Submit PR

Please take care to follow the coding style - and PEP8.

## Acknowledgements

Credit is due to **epantry** for the
[original project](https://github.com/epantry/django-netpromoterscore) from which this was forked.

Thanks also to the kind people at **Eldarion** ([website](http://eldarion.com/)) for releasing the
PyPI package name.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_nps-1.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_nps-1.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file django_nps-1.2.tar.gz.

File metadata

  • Download URL: django_nps-1.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_nps-1.2.tar.gz
Algorithm Hash digest
SHA256 5f5a7c2087aab74e70009d3997189cf9239aea701fbb408545ed99ea5427681f
MD5 2cd795f7edf3b44bb7a41c785fb79c51
BLAKE2b-256 d9b009d275b7cb9c648611e835ea9870924fbfb439c486abcb1f67f2e0a60639

See more details on using hashes here.

File details

Details for the file django_nps-1.2-py3-none-any.whl.

File metadata

  • Download URL: django_nps-1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_nps-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 002c57318e0ec0dcee103d1191a7d882f28401ac43894e8f225c7ba50d013a03
MD5 e2a8b9708411c31cd59e7b41e89c9e27
BLAKE2b-256 a427438d9a88faf94f1eb8f3ed998080dad8e776b6238df14a1a957770055e3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page