Skip to content

Commit

Permalink
Update Hubpost tracking form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Dec 16, 2024
1 parent 0553062 commit 0d3fe0c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
24 changes: 18 additions & 6 deletions backend/danswer/auth/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,16 @@ async def create(
safe: bool = False,
request: Optional[Request] = None,
) -> User:
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
)
if referral_source:
await fetch_ee_implementation_or_noop(
"danswer.server.tenants.provisioning",
"submit_to_hubspot",
)(user_create.email, referral_source, request)

tenant_id = await fetch_ee_implementation_or_noop(
"danswer.server.tenants.provisioning",
Expand Down Expand Up @@ -297,9 +304,14 @@ 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
)
if referral_source:
await fetch_ee_implementation_or_noop(
"danswer.server.tenants.provisioning",
"submit_to_hubspot",
)(account_email, referral_source, request)

tenant_id = await fetch_ee_implementation_or_noop(
"danswer.server.tenants.provisioning",
Expand Down
3 changes: 3 additions & 0 deletions backend/ee/danswer/configs/app_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
OPENAI_DEFAULT_API_KEY = os.environ.get("OPENAI_DEFAULT_API_KEY")
ANTHROPIC_DEFAULT_API_KEY = os.environ.get("ANTHROPIC_DEFAULT_API_KEY")
COHERE_DEFAULT_API_KEY = os.environ.get("COHERE_DEFAULT_API_KEY")


HUBSPOT_TRACKING_URL = os.environ.get("HUBSPOT_TRACKING_URL")
36 changes: 36 additions & 0 deletions backend/ee/danswer/server/tenants/provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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

Expand All @@ -26,6 +28,7 @@
from danswer.setup import setup_danswer
from ee.danswer.configs.app_configs import ANTHROPIC_DEFAULT_API_KEY
from ee.danswer.configs.app_configs import COHERE_DEFAULT_API_KEY
from ee.danswer.configs.app_configs import HUBSPOT_TRACKING_URL
from ee.danswer.configs.app_configs import OPENAI_DEFAULT_API_KEY
from ee.danswer.server.tenants.access import generate_data_plane_token
from ee.danswer.server.tenants.models import TenantCreationPayload
Expand Down Expand Up @@ -267,3 +270,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}")

0 comments on commit 0d3fe0c

Please sign in to comment.