Skip to content

Commit

Permalink
Update Hubpost tracking form submission (onyx-dot-app#3261)
Browse files Browse the repository at this point in the history
* Update Hubpost tracking form submission

* minor cleanup

* validated

* validate

* nit

* k
  • Loading branch information
pablonyx authored and Aron Szanto committed Dec 23, 2024
1 parent 162fe26 commit f5588b3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
2 changes: 2 additions & 0 deletions backend/ee/onyx/configs/app_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@
# when the capture is called. These defaults prevent Posthog issues from breaking the Onyx app
POSTHOG_API_KEY = os.environ.get("POSTHOG_API_KEY") or "FooBar"
POSTHOG_HOST = os.environ.get("POSTHOG_HOST") or "https://us.i.posthog.com"

HUBSPOT_TRACKING_URL = os.environ.get("HUBSPOT_TRACKING_URL")
43 changes: 41 additions & 2 deletions backend/ee/onyx/server/tenants/provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import uuid

import aiohttp # Async HTTP client
import httpx
from fastapi import HTTPException
from fastapi import Request
from sqlalchemy import select
from sqlalchemy.orm import Session

from ee.onyx.configs.app_configs import ANTHROPIC_DEFAULT_API_KEY
from ee.onyx.configs.app_configs import COHERE_DEFAULT_API_KEY
from ee.onyx.configs.app_configs import HUBSPOT_TRACKING_URL
from ee.onyx.configs.app_configs import OPENAI_DEFAULT_API_KEY
from ee.onyx.server.tenants.access import generate_data_plane_token
from ee.onyx.server.tenants.models import TenantCreationPayload
Expand Down Expand Up @@ -47,13 +50,16 @@
logger = logging.getLogger(__name__)


async def get_or_create_tenant_id(
email: str, referral_source: str | None = None
async def get_or_provision_tenant(
email: str, referral_source: str | None = None, request: Request | None = None
) -> str:
"""Get existing tenant ID for an email or create a new tenant if none exists."""
if not MULTI_TENANT:
return POSTGRES_DEFAULT_SCHEMA

if referral_source and request:
await submit_to_hubspot(email, referral_source, request)

try:
tenant_id = get_tenant_id_for_email(email)
except exceptions.UserNotExists:
Expand Down Expand Up @@ -281,3 +287,36 @@ def configure_default_api_keys(db_session: Session) -> None:
logger.info(
"COHERE_DEFAULT_API_KEY not set, skipping Cohere embedding provider configuration"
)


async def submit_to_hubspot(
email: str, referral_source: str | None, request: Request
) -> None:
if not HUBSPOT_TRACKING_URL:
logger.info("HUBSPOT_TRACKING_URL not set, skipping HubSpot submission")
return

# HubSpot tracking cookie
hubspot_cookie = request.cookies.get("hubspotutk")

# IP address
ip_address = request.client.host if request.client else None

data = {
"fields": [
{"name": "email", "value": email},
{"name": "referral_source", "value": referral_source or ""},
],
"context": {
"hutk": hubspot_cookie,
"ipAddress": ip_address,
"pageUri": str(request.url),
"pageName": "User Registration",
},
}

async with httpx.AsyncClient() as client:
response = await client.post(HUBSPOT_TRACKING_URL, json=data)

if response.status_code != 200:
logger.error(f"Failed to submit to HubSpot: {response.text}")
24 changes: 14 additions & 10 deletions backend/onyx/auth/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,20 @@ async def create(
request: Optional[Request] = None,
) -> User:
user_count: int | None = None
referral_source = None
if request is not None:
referral_source = request.cookies.get("referral_source", None)
referral_source = (
request.cookies.get("referral_source", None)
if request is not None
else None
)

tenant_id = await fetch_ee_implementation_or_noop(
"onyx.server.tenants.provisioning",
"get_or_create_tenant_id",
"get_or_provision_tenant",
async_return_default_schema,
)(
email=user_create.email,
referral_source=referral_source,
request=request,
)

async with get_async_session_with_tenant(tenant_id) as db_session:
Expand Down Expand Up @@ -346,17 +349,18 @@ async def oauth_callback(
associate_by_email: bool = False,
is_verified_by_default: bool = False,
) -> User:
referral_source = None
if request:
referral_source = getattr(request.state, "referral_source", None)
referral_source = (
getattr(request.state, "referral_source", None) if request else None
)

tenant_id = await fetch_ee_implementation_or_noop(
"onyx.server.tenants.provisioning",
"get_or_create_tenant_id",
"get_or_provision_tenant",
async_return_default_schema,
)(
email=account_email,
referral_source=referral_source,
request=request,
)

if not tenant_id:
Expand Down Expand Up @@ -502,7 +506,7 @@ async def authenticate(
# Get tenant_id from mapping table
tenant_id = await fetch_ee_implementation_or_noop(
"onyx.server.tenants.provisioning",
"get_or_create_tenant_id",
"get_or_provision_tenant",
async_return_default_schema,
)(
email=email,
Expand Down Expand Up @@ -563,7 +567,7 @@ class TenantAwareJWTStrategy(JWTStrategy):
async def _create_token_data(self, user: User, impersonate: bool = False) -> dict:
tenant_id = await fetch_ee_implementation_or_noop(
"onyx.server.tenants.provisioning",
"get_or_create_tenant_id",
"get_or_provision_tenant",
async_return_default_schema,
)(
email=user.email,
Expand Down

0 comments on commit f5588b3

Please sign in to comment.