Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hubpost tracking form submission #3261

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
Loading