-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from RamiAwar/add-posthog-anonymous-analytics
feat: Added Posthog context manager, tracked events
- Loading branch information
Showing
24 changed files
with
295 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
backend/alembic/versions/2024_09_21_2321-3d35dcd30116_added_user_analytics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""added user analytics | ||
Revision ID: 3d35dcd30116 | ||
Revises: fa9cefccac47 | ||
Create Date: 2024-09-21 23:21:13.944649 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "3d35dcd30116" | ||
down_revision: Union[str, None] = "fa9cefccac47" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
with op.batch_alter_table("user", schema=None) as batch_op: | ||
batch_op.add_column(sa.Column("analytics_enabled", sa.Boolean(), server_default=sa.text("1"), nullable=False)) | ||
|
||
|
||
def downgrade() -> None: | ||
with op.batch_alter_table("user", schema=None) as batch_op: | ||
batch_op.drop_column("analytics_enabled") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from typing import cast | ||
|
||
from posthog import Posthog | ||
from posthog.client import Client as PosthogClient | ||
|
||
from dataline.config import EnvironmentType, config | ||
from dataline.models.user.model import UserModel | ||
from dataline.repositories.base import SessionCreator | ||
from dataline.repositories.user import UserRepository | ||
|
||
logger = logging.getLogger(__name__) | ||
posthog: PosthogClient = Posthog( # type: ignore[no-untyped-call] | ||
project_api_key="phc_bcTdZnbv2IDSiMOAq3UdnHwfJlZvTF0e5ctPMJUzw0i", | ||
host="https://eu.i.posthog.com", | ||
timeout=1, # if more than 1 second, drop it, not worth making the user wait | ||
) | ||
|
||
if config.environment == EnvironmentType.development: | ||
posthog.debug = True | ||
|
||
|
||
class PosthogAnalytics: | ||
""" | ||
Context manager to use Posthog analytics in a safe way with a single point of control. | ||
If a user has disabled analytics, this context manager will not execute the block of code. | ||
""" | ||
|
||
async def __aenter__(self) -> tuple[PosthogClient, UserModel]: | ||
async with SessionCreator.begin() as session: | ||
user_repo = UserRepository() | ||
user_info = await user_repo.get_one_or_none(session) | ||
is_enabled = ( | ||
user_info is not None | ||
and user_info.analytics_enabled | ||
and config.environment == EnvironmentType.production # disable in dev mode | ||
) | ||
|
||
if not is_enabled: | ||
posthog.disabled = True | ||
else: | ||
posthog.disabled = False | ||
|
||
user_info = cast(UserModel, user_info) | ||
return posthog, user_info | ||
|
||
async def __aexit__(self, exc_type: Exception, exc_val: Exception, exc_tb: Exception) -> None: | ||
pass |
Oops, something went wrong.