diff --git a/allianceauth/analytics/middleware.py b/allianceauth/analytics/middleware.py index d3876dee..ea636b6c 100644 --- a/allianceauth/analytics/middleware.py +++ b/allianceauth/analytics/middleware.py @@ -4,6 +4,8 @@ from django.utils.deprecation import MiddlewareMixin from .models import AnalyticsTokens, AnalyticsIdentifier from .tasks import send_ga_tracking_web_view +import re + class AnalyticsMiddleware(MiddlewareMixin): def process_response(self, request, response): @@ -20,7 +22,13 @@ class AnalyticsMiddleware(MiddlewareMixin): if token.send_page_views is False: continue # Check Exclusions - if request.path in token.ignore_paths.all(): + ignore = False + for ignore_path in token.ignore_paths.values(): + ignore_path_regex = re.compile(ignore_path["ignore_path"]) + if re.search(ignore_path_regex, request.path) is not None: + ignore = True + + if ignore is True: continue tracking_id = token.token diff --git a/allianceauth/analytics/migrations/0004_auto_20211015_0502.py b/allianceauth/analytics/migrations/0004_auto_20211015_0502.py new file mode 100644 index 00000000..c9da3708 --- /dev/null +++ b/allianceauth/analytics/migrations/0004_auto_20211015_0502.py @@ -0,0 +1,31 @@ +# Generated by Django 3.1.13 on 2021-10-15 05:02 + +from django.db import migrations + + +def modify_aa_team_token_add_page_ignore_paths(apps, schema_editor): + # We can't import the Person model directly as it may be a newer + # version than this migration expects. We use the historical version. + + AnalyticsPath = apps.get_model('analytics', 'AnalyticsPath') + admin = AnalyticsPath.objects.create(ignore_path=r"^\/admin\/.*") + user_notifications_count = AnalyticsPath.objects.create(ignore_path=r"^\/user_notifications_count\/.*") + + Tokens = apps.get_model('analytics', 'AnalyticsTokens') + token = Tokens.objects.get(token="UA-186249766-2") + token.ignore_paths.add(admin, user_notifications_count) + + +def undo_modify_aa_team_token_add_page_ignore_paths(apps, schema_editor): + # nothing should need to migrate away here? + return True + + +class Migration(migrations.Migration): + + dependencies = [ + ('analytics', '0003_Generate_Identifier'), + ] + + operations = [migrations.RunPython(modify_aa_team_token_add_page_ignore_paths, undo_modify_aa_team_token_add_page_ignore_paths) + ] diff --git a/allianceauth/analytics/models.py b/allianceauth/analytics/models.py index 508435af..bc290162 100644 --- a/allianceauth/analytics/models.py +++ b/allianceauth/analytics/models.py @@ -20,7 +20,7 @@ class AnalyticsIdentifier(models.Model): class AnalyticsPath(models.Model): - ignore_path = models.CharField(max_length=254, default="/example/") + ignore_path = models.CharField(max_length=254, default="/example/", help_text="Regex Expression, If matched no Analytics Page View is sent") class AnalyticsTokens(models.Model): diff --git a/allianceauth/analytics/signals.py b/allianceauth/analytics/signals.py index 39ff87af..b3943220 100644 --- a/allianceauth/analytics/signals.py +++ b/allianceauth/analytics/signals.py @@ -20,7 +20,7 @@ def process_failure_signal( action = sender.__name__ - label = f"{exception.__class__.__name__}: {str(exception)}" + label = f"{exception.__class__.__name__}" analytics_event(category=category, action=action,