user signup referrer (#1105)

see last PR
This commit is contained in:
Brandon Thomas authored and GitHub committed 2026-03-05 20:07:38 -05:00
1 parent 13d1afd88d
commit f342befb0b
10 files changed
+60 -5

No files matched your search

@@ -1,12 +1,12 @@
{
"db_name": "MySQL",
"query": "\nINSERT INTO users\nSET\n token = ?,\n \n is_temporary = ?,\n \n username = ?,\n display_name = ?,\n\n username_is_generated = ?,\n username_is_not_customized = ?,\n\n email_address = ?,\n email_gravatar_hash = ?,\n\n email_confirmed = FALSE,\n email_confirmed_by_google = ?,\n email_is_synthetic = ?,\n \n was_eagerly_provisioned = ?,\n\n profile_markdown = ?,\n profile_rendered_html = ?,\n user_role_slug = ?,\n\n password_hash = ?,\n is_without_password = ?,\n\n maybe_feature_flags = ?,\n\n ip_address_creation = ?,\n ip_address_last_login = ?,\n ip_address_last_update = ?,\n\n maybe_source = ?,\n\n maybe_signup_method = ?\n ",
"query": "\nINSERT INTO users\nSET\n token = ?,\n \n is_temporary = ?,\n \n username = ?,\n display_name = ?,\n\n username_is_generated = ?,\n username_is_not_customized = ?,\n\n email_address = ?,\n email_gravatar_hash = ?,\n\n email_confirmed = FALSE,\n email_confirmed_by_google = ?,\n email_is_synthetic = ?,\n \n was_eagerly_provisioned = ?,\n\n profile_markdown = ?,\n profile_rendered_html = ?,\n user_role_slug = ?,\n\n password_hash = ?,\n is_without_password = ?,\n\n maybe_feature_flags = ?,\n\n ip_address_creation = ?,\n ip_address_last_login = ?,\n ip_address_last_update = ?,\n\n maybe_source = ?,\n\n maybe_signup_method = ?,\n\n maybe_referral_url = ?\n ",
"describe": {
"columns": [],
"parameters": {
"Right": 22
"Right": 23
},
"nullable": []
},
"hash": "56746bcb993cb33f5492f401ce4fc3fd44d91fed660c2f106cb22e482dffa740"
"hash": "08996721482fe897c2721298c04cf0eba5fcf1d04d57397b8afab3dd59feaa8c"
}
@@ -11,6 +11,11 @@ pub struct StripeArtcraftCreateSubscriptionCheckoutWithUserSignupRequest {
pub plan: Option<ArtcraftSubscriptionSlug>,
pub cadence: Option<PlanBillingCadence>,
/// Optional: The referral URL the user arrived from when signing up.
/// The browser can send `document.referrer` to the backend so we know how people are finding us.
/// If the browser doesn't send this parameter, we'll try the `referer` header.
pub maybe_referral_url: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema, Debug, Copy, Clone)]
@@ -62,6 +62,7 @@ async fn seed_user(
password_hash: &password_hash,
ip_address: "127.0.0.1",
maybe_source: None,
maybe_referral_url: None,
maybe_user_token: maybe_user_token.as_ref(),
}).await.map_err(|err| anyhow!("err: {:?}", err))?;
@@ -20,6 +20,11 @@ pub struct CreateAccountFromEmailPasswordArgs<'a> {
pub ip_address: &'a str,
pub maybe_source: Option<UserSignupSource>,
/// The referral URL the user arrived from when signing up.
/// The browser can send `document.referrer` to the backend so we know how people are finding us.
/// If the browser doesn't send this parameter, we'll try the `referer` header.
pub maybe_referral_url: Option<String>,
/// In production code, send this as `None`.
/// Only provide an external user token for db integration tests and db seeding tools.
/// This allows for knowing the user token a priori.
@@ -62,9 +67,11 @@ pub async fn create_account_from_email_and_password(
password_hash: args.password_hash,
is_without_password: false,
maybe_referral_url: args.maybe_referral_url,
// NB: This is just for testing.
maybe_user_token: args.maybe_user_token,
// These flags represent eagerly created users and are false for this flow
is_temporary: false,
email_is_synthetic: false,
@@ -57,6 +57,9 @@ pub async fn create_account_from_google_sso(
password_hash: SSO_PASSWORD,
is_without_password: true,
// TODO: Necessary? We're not doing SSO for now.
maybe_referral_url: None,
// NB: This is just for testing.
maybe_user_token: None,
@@ -26,6 +26,7 @@ pub struct CreateAccountFromStripeCheckoutArgs<'a> {
pub ip_address: &'a str,
pub maybe_source: Option<UserSignupSource>,
pub maybe_referral_url: Option<String>,
/// Comma separated string of feature flags.
pub maybe_feature_flags: Option<&'a str>,
@@ -68,6 +69,8 @@ pub async fn create_account_from_stripe_checkout(
password_hash: SSO_PASSWORD,
is_without_password: true,
maybe_referral_url: args.maybe_referral_url,
// NB: This is just for testing.
maybe_user_token: None,
},
@@ -60,6 +60,11 @@ pub struct GenericCreateAccountArgs<'a> {
/// Comma separated string of feature flags.
pub maybe_feature_flags: Option<&'a str>,
/// The referral URL the user arrived from when signing up.
/// The browser can send `document.referrer` to the backend so we know how people are finding us.
/// If the browser doesn't send this parameter, we'll try the `referer` header.
pub maybe_referral_url: Option<String>,
/// In production code, send this as `None`.
/// Only provide an external user token for db integration tests and db seeding tools.
/// This allows for knowing the user token a priori.
@@ -124,7 +129,9 @@ SET
maybe_source = ?,
maybe_signup_method = ?
maybe_signup_method = ?,
maybe_referral_url = ?
"#,
user_token.as_str(),
@@ -159,6 +166,8 @@ SET
args.maybe_source.map(|s| s.to_str()),
args.maybe_signup_method.map(|m| m.to_str()),
args.maybe_referral_url,
);
@@ -85,6 +85,15 @@ pub async fn stripe_artcraft_create_subscription_checkout_with_user_signup_handl
CommonWebError::ServerError
})?;
let maybe_referral_url = request.maybe_referral_url.clone()
.or_else(|| {
http_request.headers().get("referer")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
})
.filter(|s| !s.is_empty())
.map(|s| s.chars().take(255).collect::<String>());
let maybe_user_metadata = internal_user_lookup
.lookup_user_from_http_request_and_mysql_connection(&http_request, &mut mysql_connection)
.await
@@ -101,6 +110,7 @@ pub async fn stripe_artcraft_create_subscription_checkout_with_user_signup_handl
&price_id,
&mut mysql_connection,
&stripe_config,
maybe_referral_url,
).await?
},
Some(user_metadata) => {
@@ -28,6 +28,7 @@ pub (super) async fn user_creation_case(
price_id: &PriceId,
mysql_connection: &mut PoolConnection<MySql>,
stripe_config: &ArtcraftStripeConfigWithClient,
maybe_referral_url: Option<String>,
) -> Result<CreationPayload, CommonWebError> {
let mut transaction = mysql_connection.begin()
@@ -82,6 +83,7 @@ pub (super) async fn user_creation_case(
maybe_feature_flags: Some(&user_feature_flags),
ip_address: &ip_address,
maybe_source,
maybe_referral_url: maybe_referral_url.clone(),
},
Transactor::for_transaction(&mut transaction),
).await;
@@ -41,6 +41,11 @@ pub struct CreateAccountRequest {
/// Optional: Source of the signup, e.g. "artcraft", "fakeyou", "storyteller", etc.
/// If not provided, we try to infer it from the Origin header instead.
pub signup_source: Option<UserSignupSource>,
/// Optional: The referral URL the user arrived from when signing up.
/// The browser can send `document.referrer` to the backend so we know how people are finding us.
/// If the browser doesn't send this parameter, we'll try the `referer` header.
pub maybe_referral_url: Option<String>,
}
#[derive(ToSchema, Serialize)]
@@ -195,6 +200,15 @@ pub async fn create_account_handler(
maybe_source = get_request_signup_source_enum(&http_request);
}
let maybe_referral_url = request.maybe_referral_url.clone()
.or_else(|| {
http_request.headers().get("referer")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
})
.filter(|s| !s.is_empty())
.map(|s| s.chars().take(255).collect::<String>());
let create_account_result = create_account_from_email_and_password(
&mysql_pool,
CreateAccountFromEmailPasswordArgs {
@@ -205,6 +219,7 @@ pub async fn create_account_handler(
password_hash: &password_hash,
ip_address: &ip_address,
maybe_source,
maybe_referral_url,
maybe_user_token: None, // NB: This parameter is for internal testing only
}
).await;