Skip to content

Commit

Permalink
hotfix/ allow multiple identify calls (#5976)
Browse files Browse the repository at this point in the history
* allow multiple `identify` calls

* Update posthog_handler.py
  • Loading branch information
tehcoderer authored Jan 19, 2024
1 parent 801ebb2 commit 237defa
Showing 1 changed file with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def settings(self) -> LoggingSettings:

@settings.setter
def settings(self, settings: LoggingSettings) -> None:
"""Set logging settings."""
self._settings = settings

def emit(self, record: logging.LogRecord):
Expand All @@ -51,6 +52,29 @@ def emit(self, record: logging.LogRecord):
if Env().DEBUG_MODE:
raise e

def distinct_id(self) -> str:
"""Get distinct id."""
return self._settings.user_id or self._settings.app_id

def identify(self) -> None:
"""Identify user."""
if self.logged_in or not self._settings.user_id:
return

posthog.identify(
self._settings.user_id,
{
"email": self._settings.user_email,
"primaryUsage": self._settings.user_primary_usage,
},
)

if self._settings.sub_app_name == "pro":
return

self.logged_in = True
posthog.alias(self._settings.user_id, self._settings.app_id)

def log_to_dict(self, log_info: str) -> dict:
"""Log to dict."""
log_regex = r"(STARTUP|CMD|ERROR): (.*)"
Expand Down Expand Up @@ -83,22 +107,8 @@ def send(self, record: logging.LogRecord):
if event_name not in [e.value for e in self.AllowedEvents]:
return

if not self.logged_in and self._settings.user_id:
self.logged_in = True
posthog.identify(
self._settings.user_id,
{
"email": self._settings.user_email,
"primaryUsage": self._settings.user_primary_usage,
},
)
posthog.alias(self._settings.user_id, self._settings.app_id)

posthog.capture(
self._settings.app_id,
event_name,
properties=log_extra,
)
self.identify()
posthog.capture(self.distinct_id(), event_name, properties=log_extra)

def extract_log_extra(self, record: logging.LogRecord) -> Dict[str, Any]:
"""Extract log extra from record."""
Expand Down

0 comments on commit 237defa

Please sign in to comment.