Skip to content

Commit

Permalink
Added ability to enable/disable via variable
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyWW committed Mar 22, 2024
1 parent 1054fc9 commit ebce678
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion django_umami/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ class UmamiResponse:

@dataclass
class UmamiConfig:
enabled: bool
host_url: str
website_id: str

def set_enabled(self, enabled: bool):
self.enabled = enabled

def set_host_url(self, host_url: str):
self.host_url = host_url

Expand Down Expand Up @@ -52,6 +56,8 @@ class Umami:
options: UmamiConfig

def check_website_settings(self):
if not self.options.enabled:
return UmamiResponse(False, "Tracking is disabled.")
if not self.options.host_url or not self.options.website_id:
print(
"Failed to send event to umami. Please set both UMAMI_PAGE_URL and UMAMI_WEBSITE_ID vars.", flush=True
Expand Down Expand Up @@ -89,8 +95,9 @@ def track(self, event: UmamiEventData | str, event_data=None):

MAIN_PAGE_URL = get_setting("UMAMI_PAGE_URL", "")
MAIN_WEBSITE_ID = get_setting("UMAMI_WEBSITE_ID", "")
ENABLED = get_setting("UMAMI_TRACKING_ENABLED", False)

umami = Umami(options=UmamiConfig(host_url=MAIN_PAGE_URL, website_id=MAIN_WEBSITE_ID))
umami = Umami(options=UmamiConfig(enabled=ENABLED, host_url=MAIN_PAGE_URL, website_id=MAIN_WEBSITE_ID))

"""
Usage:
Expand Down
5 changes: 4 additions & 1 deletion django_umami/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ def setUpClass(cls):
# Mock settings attributes
settings.UMAMI_PAGE_URL = "https://example.com"
settings.UMAMI_WEBSITE_ID = "123456"
settings.UMAMI_ENABLED = True

@classmethod
def tearDownClass(cls):
pass

def setUp(self):
self.umami_config = UmamiConfig(host_url=settings.UMAMI_PAGE_URL, website_id=settings.UMAMI_WEBSITE_ID)
self.umami_config = UmamiConfig(
host_url=settings.UMAMI_PAGE_URL, website_id=settings.UMAMI_WEBSITE_ID, enabled=settings.UMAMI_ENABLED
)
self.umami = Umami(options=self.umami_config)

def test_umami_config(self):
Expand Down

0 comments on commit ebce678

Please sign in to comment.