Skip to content

Commit 3aaad32

Browse files
committed
Add IGNORE_URLS for SendPageViewEvent
1 parent e3236f8 commit 3aaad32

File tree

9 files changed

+237
-173
lines changed

9 files changed

+237
-173
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ MIDDLEWARE = [
6464
]
6565
```
6666

67+
While using `SendPageViewEvent` if there are certain URLs that you do not want to update Amplitude with you can use the `AMPLITUDE_IGNORE_URLS` setting. This setting take a list of relative urls or URL names. Relative URLS most match exactly so need to start with a forward slash (`/`)
68+
69+
```python
70+
AMPLITUDE_IGNORE_URLS = ['my_url_name', '/testurl']
71+
```
72+
6773

6874
### Sending events manually
6975

amplitude/middleware.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from time import time
33
from uuid import uuid4
44

5-
from . import Amplitude
5+
from django.urls import resolve
6+
7+
from . import Amplitude, settings
68
from .amplitude import AmplitudeException
79

810
log = logging.getLogger(__name__)
@@ -26,6 +28,13 @@ def __init__(self, get_response):
2628
self.get_response = get_response
2729

2830
def __call__(self, request):
31+
# Ignore URLs can be either a url path or url name
32+
if request.path_info in settings.IGNORE_URLS:
33+
return self.get_response(request)
34+
current_url = resolve(request.path_info)
35+
if current_url.url_name in settings.IGNORE_URLS:
36+
return self.get_response(request)
37+
2938
event = amplitude.build_event_data(
3039
event_type='Page view', request=request
3140
)

amplitude/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
INCLUDE_USER_DATA = getattr(settings, 'AMPLITUDE_INCLUDE_USER_DATA', False) # NOQA: E501
99
INCLUDE_GROUP_DATA = getattr(settings, 'AMPLITUDE_INCLUDE_GROUP_DATA', False) # NOQA: E501
10+
IGNORE_URLS = getattr(settings, 'AMPLITUDE_IGNORE_URLS', [])
11+
if not isinstance(IGNORE_URLS, list):
12+
error = '"AMPLITUDE_IGNORE_URLS" must be a list of URLs or URL names'
13+
raise ImproperlyConfigured(error)
1014

1115
installed_apps = getattr(settings, 'INSTALLED_APPS')
1216
middleware = getattr(settings, 'MIDDLEWARE')

0 commit comments

Comments
 (0)