Actually, the first step might be the one that requires the first consent. So we show the first step and on submission we record the data and also record that the user consented to that category? But we need explicit consent for each category? We can have a separate consent record.
We can design:
First, we show a consent banner for the first category (or multiple categories). When the user accepts, we record the consent and then show the first step.
Alternatively, we can have the first step include a checkbox for consent? But that might be less clear.
Let's do:
Step 0: Consent for the first category (or multiple categories). We show a consent form that lists the categories and what data we will collect in the first step. Then, on acceptance, we record the consent and then show the first step.
But then the first step is separate.
Alternatively, we can have the consent as part of the first step? But then if the user doesn't submit the step, we don't have consent? We need consent before collecting data.
So:
We have a consent management system. We ask for consent for categories. We can ask for multiple categories at once? But the idea of progressive profiling is to ask for one category at a time? Not necessarily.
Let's assume we ask for one category at a time, and each step is tied to a category.
Then:
Initial: we ask for consent for the first category (step1's category). If the user gives consent, we show step1. If the user doesn't give consent, we don't show step1 and we don't have consent for that category.
Then, when step1 is completed, we ask for consent for the next category (step2's category) and then show step2.
But that means we are asking for consent right before each step? That might be acceptable.
Alternatively, we can ask for all consents at the beginning? But then we are not progressive in consent.
Given the requirement of progressive profiling, we ask for consent just before we need it.
So:
For each step, we check if we have consent for that step's category. If not, we ask for consent (maybe in a separate screen) and then show the step.
But then we are interleaving consent requests and data collection.
We can design:
The profiling flow is a sequence of steps, each step has:
- Optional: consent request for its category (if not already consented)
- Then the form for the step.
So we might have a step that is just consent, or we can combine.
Let's make it:
Each step has:
- category: the category of data we are going to collect in this step.
- fields: the fields to collect.
And we have a separate consent record per category.
The flow for a step:
if user has not consented to step.category:
show consent form for step.category (with description of what we will collect and why)
on consent given: record consent and then show the step form.
else:
show the step form.
Then, when the step form is submitted, we collect the data and mark the step as completed.
But note: the consent form is part of the step? Then the step is not completed until both consent and data are collected? Actually, the consent is a prerequisite.
So we can break the step into two parts:
Part 1: Get consent for the category (if not already given).
Part 2: Collect the data for the step.
And we track:
- For each step, we have a status: consent_pending, data_pending, completed.
But that complicates the step completion. Alternatively, we can consider that the step is completed when the data is collected and we have consent. But if we already had consent, then the step is just data collection.
Let's simplify:
We don't track step status separately. We only track:
- For each category, whether we have consent.
- For each step, whether we have collected the required data.
And the step is considered completed when:
- We have consent for the step's category AND
- For every required field in the step, we have data.
So the step might be in a state where we have consent but not all data? Then we show the step form.
And if we don't have consent, we show the consent form first.
So the flow for the next step:
step = get_next_step()
if not step:
profiling complete (or no more steps with consent)
else:
if user_has_consent(step.category):
show step form
else:
show consent form for step.category (and on consent, show step form)
But note: what if the user gives consent but then doesn't complete the step? Then we have consent but no data. The step is not completed, so next time we will show the step form again (because we have consent and step not completed).
That's acceptable.
Now, dropout analytics:
We want to track:
- When we show the consent form for a category (step_started? but it's not the step yet)
- When the consent is given (consent_given event)
- When we show the step form (step_started)
- When the step form is completed (step_completed)
- If the user leaves after seeing the consent form but before giving consent? We can track as consent_abandoned?
- If the user leaves after seeing the step form but before submitting? We can track as step_abandoned.
We can have:
event_types = [
"consent_form_shown",
"consent_given",
"consent_abandoned", # when the user leaves the consent form without giving consent
"step_form_shown",
"step_completed",
"step_abandoned" # when the user leaves the step form without submitting
]
But note: the step form might be shown after consent, so we might have both events for one step.
Alternatively, we can have:
For each step, we track:
- consent_shown: if we showed the consent form for this step's category.
- consent_given: if the user gave consent for this step's category (after being shown).
- step_shown: if we showed the step form.
- step_completed: if the step form was completed.
But we are tracking per user and per step? Or per category? Since one category might be used in multiple steps? We said one category per step, but what if we have multiple steps for the same category? Then we might ask consent once and then show multiple steps? That's possible.
So we need to track per category and per step.
Given the complexity, let's assume one category per step and each category is only used in one step. Then we can track by step.
But to be general, we can track:
- For each category, we have consent events.
- For each step, we have step events.
And then we can analyze:
- Dropout at consent: if we show consent and the user doesn't give consent and leaves, that's a dropout at consent.
- Dropout at step: if we show the step form and the user doesn't complete, that's a dropout at step.
How do we know the user left? We can use the browser's beforeunload event to send an abandoned event? But that might not be reliable.
Alternatively, we can set a timeout on the server? But we don't know when the user leaves.
A common approach is to track the step as started when we show the form, and then if we don't get a completed event within a certain time (or if the user navigates away within the site) we consider it abandoned. But if the user completes in a new session, we might have a step started in one session and completed in another.
We can do:
When we show the step form (or consent form), we create an "in-progress" record with a timestamp.
When the user completes, we mark that in-progress record as completed.
We can have a cron job that looks for in-progress records older than, say, 30 minutes and marks them as abandoned? But that's not real-time.
Alternatively, we can use the client to send an "abandon" event when the user leaves the page (using the Page Visibility API or beforeunload). But beforeunload is not always reliable and might not fire if the browser crashes.
Given the constraints, let's do:
- We log step_started (or consent_shown) when we render the form.
- We log step_completed (or consent_given) when the form is submitted.
- We do not log abandon events. Instead, we analyze by looking at the time between step_started and step_completed. If there's no step_completed within a reasonable time (or the user never comes back), we consider it a dropout. But we don't know if the user will come back.
This is a common problem in analytics. We can define a conversion funnel and measure the dropoff as the number of users who started but didn't complete within, say, 7 days? But then we have to wait 7 days to know.
Alternatively, we can track session by session. If the user starts a step in one session and doesn't complete, and then in the next session we show the same step again, that's a dropout from the previous session? But we don't know if they abandoned intentionally.
Given the complexity, let's stick to:
We log events:
- profiling_step_started: {user_id, step_id, timestamp, session_id}
- profiling_step_completed: {user_id, step_id, timestamp, session_id}
And then we can analyze by step_id and by session. We can also track the time between start and complete.
For consent, we log:
- consent_requested: {user_id, category, timestamp, step_id?}
- consent_given: {user_id, category, timestamp, step_id?}
And then we can see if the user gave consent after being requested.
We don't track explicit abandonment, but we can infer from the lack of a completed event.
Now, the implementation:
We'll have:
- A frontend that:
* On page load, checks for user_id cookie. If not present, generates one.
* Fetches the user profile from the server (or the server includes it in the page) to know the current state.
* Determines the next step to show (by calling an API endpoint that returns the next step and whether we need to show consent first).
* Renders either the consent form or the step form.
* Submits the form (consent or data) to the server.
- A backend that:
* Manages the user profile.
* Serves the next step (with the form and any existing data to prefill).
* Accepts form submissions (both consent and data) and updates the user profile.
* Logs events.
Let's design the API endpoints:
GET /api/profiling/next_step
Returns:
{
"step": {
"step_id": 1,
"title": "...",
"fields": [...],
"category": "basic_info"
},
"needs_consent": true/false, // if true, we need to show consent for the step's category first
"existing_data": {
"first_name": "John",
...
}
}
If there is no next step, return { "step": null, "message": " Profiling complete" }
POST /api/profiling/consent
Body: { "category": "basic_info", "granted": true, "version": "1.0" }
Records the consent and logs event.
POST /api/profiling/step
Body: { "step_id": 1, "data": { "first_name": "John", ... } }
Validates that the user has consent for the step's category? (We might skip if we already checked in the frontend, but we should check on the server too.)
Stores the data and marks the step as completed.
Logs event.
But note: the step might be submitted without having given consent? We should check on the server that the user has consent for the step's category.
Alternatively, we can have the frontend only show the step form if consent is given, but we must enforce on the server.
Now, the dropout tracking:
In the GET /api/profiling/next_step, we log a "step_started" event for the step we are about to show? But that would be when we request the next step, which might be before the user sees it? We want to log when the user actually sees the form.
We can log the step_started when the frontend renders the form? But then we need to send an event from the frontend.
Alternatively, we can log when we serve the form (in the GET next_step) and then if the user never completes, we don't know. But we can consider that as a step started.
However, if the user requests the next step but then navigates away without seeing the form, we would have a false start.
We can do:
When the frontend receives the step data and is about to render, it sends a "step_viewed" event? But that's an extra request.
Given the trade-offs, let's log the step_started when we serve the step (in the GET next_step) and then if the user completes, we log step_completed. We can consider that the step was started when we sent the form. The dropout rate would be: 1 - (completed / started). But note: a user might start multiple times? We are tracking per user per step? But a user might attempt a step multiple times? We might want to track attempts.
We can track:
For each user and step, we have multiple attempts? Or we consider only the first attempt?
Let's assume we only track the first time the user sees the step. If they abandon and then come back, we show the same step again? Yes, because it's not completed. So we would have multiple starts for the same step? We might want to track each start as an attempt.
So we log:
step_started: {user_id, step_id, attempt_number, timestamp}
But we don't have attempt number. We can increment a counter for the user and step? Or we just log every time we show the step.
Then, step_completed: {user_id, step_id, attempt_number?} but we don't know which attempt it is? We can link by having the same attempt_id? But we don't generate an attempt_id.
Alternatively, we can log:
step_started: {user_id, step_id, timestamp, session_id}
step_completed: {user_id, step_id, timestamp, session_id}
And then we can match by step_id and session_id? But the same step might be started in one session and completed in another? Then we have two starts and one completion? We want to match the start and completion that belong together.
We can generate an instance_id when we show the step and send it with the form, and then when the form is submitted, we send that instance_id. Then we can match.
So:
GET /api/profiling/next_step returns:
{
"step": ...,
"instance_id": "uuid"
}
Then, when submitting the step, we send the instance_id.
And we log:
step_started: {user_id, step_id, instance_id, timestamp, session_id}
step_completed: {user_id, step_id, instance_id, timestamp, session_id}
Then we can match by instance_id.
Similarly for consent:
When we show the consent form, we generate an instance_id and return it with the consent form? But the consent form is shown as part of the next_step? Or separately?
We have two separate forms: consent and step. So we need two instance_ids? Or we can have one for the step that includes the consent? But the consent and step are separate.
Alternatively, we can have:
For a step that requires consent:
We first show the consent form (with an instance_id for the consent) and then on consent given, we show the step form (with a new instance_id for the step).
Then we log:
consent_started: {user_id, category, instance_id, timestamp}
consent_given: {user_id, category, instance_id, timestamp}
step_started: {user_id, step_id, instance_id, timestamp}
step_completed: {user_id, step_id, instance_id, timestamp}
But note: the step might not be shown if the user doesn't give consent.
So the flow:
GET /api/profiling/next_step returns:
{
"step": { ... },
"needs_consent": true,
"consent_category": "basic_info",
"consent_instance_id": "uuid1",
"step_instance_id": null // we don't have the step instance yet because we haven't shown the step form
}
Then the frontend shows the consent form with the consent_instance_id.
On consent form submit:
POST /api/profiling/consent
Body: { "instance_id": "uuid1", "category": "basic_info", "granted": true }
Then the server records the consent and returns:
{
"step": { ... }, // the same step
"needs_consent": false,
"step_instance_id": "uuid2"
}
Then the frontend shows the step form with step_instance_id.
On step form submit:
POST /api/profiling/step
Body: { "instance_id": "uuid2", "step_id": 1, "data": {...} }
Then we can track:
- If the user doesn't give consent, we don't show the step and we have a consent_started but no consent_given? We can log consent_started when we show the consent form. Then if the user never gives consent, we have a consent_started without consent_given. We can consider that a dropout at consent.
- If the user gives consent but doesn't complete the step, we have consent_given and step_started (because we show the step form after consent) but no step_completed.
But note: we might not get the step_started if the user gives consent and then never loads the step form? We can log step_started when we serve the step form (in the response to consent submission). But that's the same as the step_instance_id being generated? We can log step_started when we generate the step_instance_id? But we generate it only after consent is given? Then we can log step_started at that point? But the user might not see the step form? We can log when the frontend receives the step form and is about to render? But that would require an extra event.
Alternatively, we can log step_started when we generate the step_instance_id (on the server after consent). Then if the user never loads the step form, we have a step_started without step_completed? But the step form wasn't shown. That's a false positive.
Let's change:
We log consent_started when we show the consent form (in the GET next_step when needs_consent is true). We log by having the frontend send an event when the consent form is rendered? But that's extra.
Given the complexity and the fact that we are building a simple example, let's simplify:
We won't track instance_id for now. We'll just log:
- When we show the consent form (if needed) we log consent_shown.
- When the user gives consent, we log consent_given.
- When we show the step form, we log step_shown.
- When the step is completed, we log step_completed.
And we don't try to match them. We just count:
For a step that requires consent:
We have:
consent_shown count (how many times we asked for consent)
consent_given count (how many times consent was given)
step_shown count (how many times we showed the step form after consent)
step_completed count (how many times the step was completed)
And we can calculate:
Consent conversion rate: consent_given / consent_shown
Step completion rate: step_completed / step_shown
But note: if we show the consent form multiple times to the same user (because they didn't give consent and then we ask again later?) we might show the consent form again? We are only asking once per category? Actually, if the user doesn't give consent, we don't show the step and we don't ask again? We might ask again in the future? We can decide to ask again after some time? But let's assume we ask only once per category per user.
So for a given user and category, we show the consent form at most once? Then the counts are per user? But we are aggregating across users.
This is acceptable for analytics.
So we'll do:
In GET /api/profiling/next_step:
if needs_consent:
log_event("consent_shown", {user_id, category})
return {needs_consent: true, ...}
else:
log_event("step_shown", {user_id, step_id})
return {needs_consent: false, ...}
In POST /api/profiling/consent:
log_event("consent_given", {user_id, category, granted})
if granted:
then we show the step form? Actually, we return the step with needs_consent false and step_instance? But we don't have step_instance. We just return the step.
But then we need to log step_shown? We can log it when we return the step? But that's in the response to the consent POST. So we can log step_shown in the same request? But the frontend hasn't shown the step form yet.
Alternatively, we can have the frontend log step_shown when it renders the step form? But that's an extra request.
Let's change:
In the consent POST response, we return the step data. Then the frontend shows the step form and sends a step_shown event? But then we have an extra request.
We can combine: when we return the step data in the consent POST, we log step_shown on the server? But the step might not be shown if the frontend fails? That's a risk.
Given the trade-offs, let's do:
We log step_shown when we serve the step form in the GET /api/profiling/next_step when needs_consent is false.
And when we serve the consent form (needs_consent true) we log consent_shown.
Then, when the user gives consent, we log consent_given and then we return the step form? But we haven't logged step_shown for that step? We can log step_shown in the same consent POST? But that's not the same as showing the step form? We are about to show it.
Alternatively, we can have two endpoints:
GET /api/profiling/consent?category=basic_info
Returns the consent form and logs consent_shown.
POST /api/profiling/consent
Logs consent_given and then returns the next step (which might be the step form or the next consent?).
But that complicates the flow.
Let's stick to the original plan:
GET /api/profiling/next_step:
if user has consent for the step's category:
log step_shown for that step.
return step.
else:
log consent_shown for that category.
return {needs_consent: true, category: step.category}
Then the frontend shows either consent form or step form.
On consent form submit (to a separate endpoint? or the same next_step?):
We have POST /api/profiling/consent that records the consent and then returns the same step? But then we need to log step_shown? We can log step_shown when we return the step? But that's in the consent POST response.
We can do:
In the consent POST, after recording consent, we log step_shown for the step that we are about to show? But we don't know which step? We know the category, and the step that requires that category? But there might be multiple steps? We are in the context of a step? Actually, the consent is requested for a specific step? Yes, because we are in the flow of a step.
So we can pass the step_id in the consent request? But the consent is for a category, and the step might be the one we were about to show.
Alternatively, we can have the consent request include the step_id? Then we can log step_shown for that step_id after the consent is given? But step_shown should be logged when the step form is shown, not when the consent is given.
Given the complexity, let's change the flow:
We have a single endpoint for the next step that returns either:
{ "consent_required": true, "category": "basic_info", "step_id": 1 }
or
{ "consent_required": false, "step": { ... }, "step_id": 1 }
And we log:
When we return consent_required: true, we log "consent_shown" for that category and step_id? But we don't have the step_id? We do, because we are returning the step_id? Actually, we are returning the step that requires consent. So we can log:
event: "consent_shown", {user_id, step_id, category}
Then, when the user gives consent (via a separate endpoint), we log "consent_given" and then we return the same step_id with consent_required false? But we don't have the step form data? We need to return the step form.
Alternatively, we can have the consent endpoint return the step form? But then we are mixing consent and step.
Let's do:
GET /api/profiling/next_step:
Returns:
{
"step_id": 1,
"title": "...",
"fields": [...],
"category": "basic_info",
"needs_consent": true // if the user doesn't have consent for category
}
And logs:
if needs_consent: log "consent_shown" for {user_id, step_id, category}
else: log "step_shown" for {user_id, step_id}
Then the frontend:
if needs_consent:
show consent form for category.
on submit: POST /api/profiling/consent with {step_id: 1, category: "basic_info", granted: true}
else:
show step form for step_id.
In the consent POST:
Record the consent.
Log "consent_given" for {user_id, step_id, category}
Then return the same step (without needs_consent) but with the step form? We can return the step data (fields) and mark that we are now showing the step? But we haven't shown it yet.
Then the frontend, after the consent POST, shows the step form and then we need to log "step_shown"? But we already logged "step_shown" for this step? We haven't because we only log step_shown when we return the step with needs_consent false in the GET.
We can avoid logging step_shown for the same step twice? But we want to log when the step form is actually shown.
Alternatively, we can log step_shown in the frontend when the step form is rendered? But that requires an extra event.
Given the time, let's simplify the analytics:
We only track:
- consent_shown: when we ask for consent (in GET next_step with needs_consent)
- consent_given: when the user gives consent (in POST consent)
- step_completed: when the step is completed (in POST step)
And we don't track step_shown. Then we can calculate:
Consent conversion: consent_given / consent_shown
Step completion: step_completed / consent_given (assuming we only show the step after consent is given)
But what about steps that don't require consent? Then we don't have consent_shown/consent_given. We can have a separate metric for steps without consent? We can track step_shown for those? But we don't have a step_shown event.
We can log step_shown for every step that we return (whether with consent or without) in the GET next_step. Then:
For steps that require consent:
We log consent_shown and step_shown at the same time? But we only show one at a time.
Let's define:
When we return a step in GET next_step:
if needs_consent:
log consent_shown for the step's category and step_id? But we are not showing the step form, so we don't log step_shown.
else:
log step_shown for the step_id.
Then, when the user gives consent (for a step that required consent), we don't log step_shown because we haven't shown the step form yet. We will show the step form in the next request? Actually, after the consent is given, we return the step form? We can return the step form in the consent POST response? Then we can log step_shown in that response? But that's not the same as the frontend showing it.
This is getting too messy.
Given the time, let's drop the detailed analytics and just log:
- When we show a step form (with or without consent) we log "step_shown".
- When we show a consent form (as a separate screen) we log "consent_shown".
- When the user gives consent, log "consent_given".
- When the user completes a step, log "step_completed".
And we need to make sure that:
- For a step that requires consent: we show consent form (consent_shown) and then step form (step_shown) if consent given.
- For a step that doesn't require consent: we show step form (step_shown) directly.
And we need to log these events from the frontend when the forms are actually rendered? Because if we log on the server when we serve the form, we might log for users who don't actually see the form (e.g., network error). But that's a minor issue.
We'll log on the server when we serve the form (in the API response) and assume that if we serve it, the user will see it.
So:
GET /api/profiling/next_step:
if needs_consent:
log_event("consent_shown", {user_id, category})
return {needs_consent: true, category: ..., step_id: ...}
else:
log_event("step_shown", {user_id, step_id})
return {needs_consent: false, step: ...}
Then, when the user gives consent (POST /api/profiling/consent), we log "consent_given" and then we return the step (with needs_consent false) and log "step_shown"? But we are returning the step in the consent POST response. We can log step_shown in that response? But then we are logging step_shown twice for the same step? Because the step was already returned in the GET? No, because in the GET we returned with needs_consent true, so we didn't log step_shown.
So in the consent POST, after recording consent, we log "step_shown" for the step_id and then return the step data.
Then the frontend shows the step form.
But what if the user doesn't give consent? Then we don't show the step and we don't log step_shown.
And when the user completes the step (POST /api/profiling/step), we log "step_completed".
This seems consistent.
However, note: the step_id in the consent POST? We need to know which step we are in. We can pass the step_id in the consent request? Because the consent is for a specific step? Actually, the consent is for a category, and the step is the one that requires that category. But we might have multiple steps for the same category? We said one category per step, so one step per category? Then we can get the step by category? But we are in the context of a step.
We can pass the step_id in the consent request? Then we know which step we are going to show after consent.
Let's change the GET next_step to return:
{
"step_id": 1,
"needs_consent": true,
"category": "basic_info"
}
Then the frontend, when submitting consent, sends:
{ "step_id": 1, "granted": true }
Then the server:
- Records consent for the category of step 1.
- Logs consent_given for {user_id, step_id, category}
- Logs step_shown for {user_id, step_id}
- Returns the step data for step 1 (with needs_consent false now).
Then the frontend shows the step form.
And when the step form is submitted, we send:
{ "step_id": 1, "data": {...} }
And log step_completed.
This works.
But note: what if the user changes their mind and withdraws consent? Then we need to handle that separately.
We have an endpoint for withdrawing consent:
POST /api/profiling/consent/withdraw
Body: { "category": "basic_info" }
Then we:
- Update the consent record for that category to granted=false (or remove it).
- Remove data for that category.
- Remove from profiling_steps_completed any step that requires that category.
- Log consent_withdrawn.
And then the next time we call GET /api/profiling/next_step, we will see that the step that requires that category is not completed and we don't have consent, so we will show consent again.
But note: we might have multiple steps for the same category? We said one category per step, so only one step per category. Then we remove that step from completed and we will ask consent again and then show the step again.
This is acceptable.
Now, the complete flow:
Initial:
user visits, we set a cookie with user_id (or use existing).
GET /api/profiling/next_step
Suppose step1 requires category "basic_info" and user has no consent.
Response:
{
"step_id": 1,
"needs_consent": true,
"category": "basic_info"
}
We log: consent_shown for step1 and category basic_info.
Frontend shows consent form for basic_info.
User gives consent:
POST /api/profiling/consent
{ "step_id": 1, "granted": true }
Server:
- Record consent for basic_info.
- Log consent_given for step1 and basic_info.
- Log step_shown for step1.
- Return step1 data (fields) and step_id.
Frontend shows step1 form.
User submits step1:
POST /api/profiling/step
{ "step_id": 1, "data": { "first_name": "John", ... } }
Server:
- Validate that user has consent for step1's category (basic_info) -> yes.
- Store data with category.
- Mark step1 as completed (add to profiling_steps_completed).
- Log step_completed for step1.
Then next time, GET /api/profiling/next_step will return step2 (if any).
For a step that doesn't require consent? We don't have such steps.
But what about the initial step? We are requiring consent for every step.
This seems workable.
Now, the dropout analysis:
For each step, we have:
consent_shown count (how many times we asked for consent for this step)
consent_given count (how many times consent was given for this step)
step_shown count (how many times the step form was shown for this step) -> note: step_shown is logged only after consent is given and we are about to show the step form? Actually, we log step_shown in the consent POST response. But what if the user doesn't have to give consent (because they already have)? Then we log step_shown in the GET next_step.
So:
For steps that require consent and the user hasn't consented:
We log consent_shown when we return the step with needs_consent true.
Then, if the user gives consent, we log consent_given and step_shown (in the consent POST).
Then, if the user completes, we log step_completed.
For steps that don't require consent (or user already has consent):
We log step_shown in the GET next_step.
Then, if the user completes, we log step_completed.
Then we can calculate:
Consent conversion rate for step X: consent_given / consent_shown
Step completion rate for step X: step_completed / step_shown
But note: for steps that require consent, step_shown is logged only after consent is given. So step_shown <= consent_given? Actually, step_shown is logged in the same request as consent_given? So for a given step, step_shown should equal consent_given (because we only show the step after consent is given). But what if the user already had consent? Then step_shown is logged without consent_given.
So we have two types of steps:
Type A: Steps that required consent and the user had to give it.
consent_shown -> consent_given -> step_shown -> step_completed
Type B: Steps that didn't require consent (user already had it).
step_shown -> step_completed
Then for Type A:
consent conversion rate = consent_given / consent_shown
step completion rate = step_completed / step_shown
For Type B:
step completion rate = step_completed / step_shown
We can also track the same user multiple times? We are logging per event, so we can aggregate by user and step.
This seems acceptable.
Now, let's code the backend:
We'll have:
- A user profile store (database).
- A profiling plan (static or from DB).
- An event store (for analytics).
We'll use:
UserProfile:
user_id (string, primary key)
data: JSON (field_name -> {value, category, collected_at})
consent: JSON array of {category, granted, timestamp, version}
profiling_steps_completed: array of step_ids
current_step: integer (the next step to show, or null if done)
But note: the current_step might not be accurate if the user withdraws consent? We can compute the next step on the fly.
We don't need to store current_step. We can compute the next step by iterating the profiling plan.
So we remove current_step and compute on the fly.
Steps:
GET /api/profiling/next_step:
user = get_user(user_id)
plan = get_profiling_plan()
for step in plan.steps (ordered by step_id):
if step.step_id in user.profiling_steps_completed:
continue
if not user_has_consent(user, step.category):
# This step requires consent and we don't have it.
log_event("consent_shown", {user_id, step_id: step.step_id, category: step.category})
return {
"step_id": step.step_id,
"needs_consent": true,
"category": step.category
}
else:
# We have consent, so we can show the step.
log_event("step_shown", {user_id, step_id: step.step_id})
return {
"step_id": step.step_id,
"needs_consent": false,
"step": {
"step_id": step.step_id,
"title": step.title,
"fields": step.fields
},
"existing_data": get_existing_data_for_step(user, step)
}
return { "step": null }
POST /api/profiling/consent:
body: { step_id, granted }
step = get_step(step_id)
user = get_user(user_id)
if granted:
record_consent(user, step.category)
log_event("consent_given", {user_id, step_id, category: step.category})
# Now we can show the step
log_event("step_shown", {user_id, step_id})
return {
"step_id": step.step_id,
"needs_consent": false,
"step": {
"step_id": step.step_id,
"title": step.title,
"fields": step.fields
},
"existing_data": get_existing_data_for_step(user, step)
}
else:
# User refused consent. We don't show the step.
# We might want to log? But we already logged consent_shown and now we have a refused consent? We can log consent_refused?
# But we are not tracking refusal. We can log as consent_given with granted=false? Or a separate event.
# Let's log consent_given with granted=false.
log_event("consent_given", {user_id, step_id, category: step.category, granted: false})
return { "message": "Consent not given" }
POST /api/profiling/step:
body: { step_id, data }
step = get_step(step_id)
user = get_user(user_id)
# Check consent
if not user_has_consent(user, step.category):
return error 403
# Store the data for each field in the step.
for field in step.fields:
if field.name in data:
# Store with category
user.data[field.name] = {
"value": data[field.name],
"category": step.category,
"collected_at": now()
}
# Mark step as completed
if step.step_id not in user.profiling_steps_completed:
user.profiling_steps_completed.append(step.step_id)
save_user(user)
log_event("step_completed", {user_id, step_id})
return { "success": true }
POST /api/profiling/consent/withdraw:
body: { category }
user = get_user(user_id)
# Update consent
for c in user.consent:
if c.category == category:
c.granted = False
# We might want to remove the consent record? Or keep with granted=False.
# Let's keep with granted=False.
# Remove data for this category
fields_to_remove = [field_name for field_name, d in user.data.items() if d.category == category]
for field_name in fields_to_remove:
del user.data[field_name]
# Remove completed steps that require this category
plan = get_profiling_plan()
steps_to_remove = []
for step in plan.steps:
if step.category == category:
if step.step_id in user.profiling_steps_completed:
user.profiling_steps_completed.remove(step.step_id)
save_user(user)
log_event("consent_withdrawn", {user_id, category})
return { "success": true }
This is a basic implementation.
Note: We are not handling the case where a step has multiple categories? We assumed one category per step.
Also, we are not handling the case where a field might have a different category than the step? We are using the step's category for all fields in the step.
If we want per-field category, we would need to adjust.
Given the time, we'll stick to one category per step.
Now, the frontend:
On page load:
fetch('/api/profiling/next_step')
.then(response => response.json())
.then(data => {
if (data.step) {
if (data.needs_consent) {
showConsentForm(data.category, data.step_id);
} else {
showStepForm(data.step, data.existing_data);
}
} else {
// No more steps
console.log("Profiling complete");
}
});
When consent form is submitted:
fetch('/api/profiling/consent', {
method: 'POST',
body: JSON.stringify({ step_id: stepId, granted: true })
})
.then(response => response.json())
.then(data => {
if (data.step) {
showStepForm(data.step, data.existing_data);
} else {
// Something went wrong?
}
});
When step form is submitted:
fetch('/api/profiling/step', {
method: 'POST',
body: JSON.stringify({ step_id: stepId, data: formData })
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Go to next step
loadNextStep();
}
});
This is a basic flow.
We also need to handle the case where the user withdraws consent. We can have a link to withdraw consent that calls the withdraw endpoint and then reloads the next step.
Now, the analytics:
We are logging events on the server. We can store them in a database or send to an analytics service.
We can then query:
For each step_id:
count of consent_shown events
count of consent_given events (with granted=true)
count of step_shown events
count of step_completed events
And calculate the rates.
This design meets the requirements of progressive profiling, consent, and dropout analytics.
Let's note some caveats:
- We are using a simple cookie for user identification. This might not be persistent if the user clears cookies. We might want to use a more persistent identifier if the user logs in.
- We are not encrypting the data at rest. We should, especially for personal data.
- We are not handling data deletion upon consent withdrawal immediately? We are removing the data from the user profile, but there might be logs? We should also delete the events? But events are for analytics and might be anonymized? We need to consider GDPR.
- We are not providing a way to view or correct the data. We should have an endpoint for the user to see their data and edit? But progressive profiling is about collecting over time. We might want to allow the user to edit their data at any time.
- We are not handling the case where the user partially completes a step and then leaves? We only mark the step as completed when all required fields are submitted. The user might fill some fields and leave? We are not saving partial data? We could save partial data and prefill next time? But then we might be storing data without consent? We only store when the step is completed? But what if the user has consent and fills some fields and leaves? We don't save until they submit. That's acceptable.
- We are not handling validation of the step form (required fields, etc.). We should validate on the server.
- We are not handling the case where the profiling plan changes? If we add a new step, we will show it to existing users? That might be okay.
This is a basic design. We can iterate.
Let's write the code for the backend in Python (Flask) for example:
from flask import Flask, request, jsonify
import uuid
from datetime import datetime
app = Flask(__name__)
# In-memory stores for example
user_profiles = {} # user_id -> user_profile dict
events = [] # list of event dicts
profiling_plan = [
{
"step_id": 1,
"title": "Basic Info",
"category": "basic_info",
"fields": [
{"name": "first_name", "label": "First Name", "type": "text", "required": True},
{"name": "last_name", "label": "Last Name", "type": "text", "required": True}
]
},
{
"step_id": 2,
"title": "Address",
"category": "address_info",
"fields": [
{"name": "address", "label": "Address", "type": "text", "required": True},
{"name": "city", "label": "City", "type": "text", "required": True}
]
}
]
def get_user(user_id):
if user_id not in user_profiles:
# Create a new user profile
user_profiles[user_id] = {
"user_id": user_id,
"data": {},
"consent": [],
"profiling_steps_completed": []
}
return user_profiles[user_id]
def user_has_consent(user, category):
for c in user.get('consent', []):
if c['category'] == category and c['granted']:
return True
return False
def record_consent(user, category):
# Update or add consent
found = False
for c in user.get('consent', []):
if c['category'] == category:
c['granted'] = True
c['timestamp'] = datetime.utcnow().isoformat()
found = True
break
if not found:
user.setdefault('consent', []).append({
'category': category,
'granted': True,
'timestamp': datetime.utcnow().isoformat(),
'version': '1.0'
})
def log_event(event_type, data):
events.append({
'event_type': event_type,
'timestamp': datetime.utcnow().isoformat(),
**data
})
def get_existing_data_for_step(user, step):
# Return a dict of field_name -> value for fields that are in the step and already collected.
existing = {}
for field in step['fields']:
if field['name'] in user.get('data', {}):
existing[field['name']] = user['data'][field['name']]['value']
return existing
@app.route('/api/profiling/next_step', methods=['GET'])
def next_step():
user_id = request.cookies.get('user_id')
if not user_id:
user_id = str(uuid.uuid4())
# In a real app, set the cookie in the response.
user = get_user(user_id)
for step in profiling_plan:
if step['step_id'] in user.get('profiling_steps_completed', []):
continue
if not user_has_consent(user, step['category']):
log_event('consent_shown', {'user_id': user_id, 'step_id': step['step_id'], 'category': step['category']})
return jsonify({
'step_id': step['step_id'],
'needs_consent': True,
'category': step['category']
})
else:
log_event('step_shown', {'user_id': user_id, 'step_id': step['step_id']})
return jsonify({
'step_id': step['step_id'],
'needs_consent': False,
'step': step,
'existing_data': get_existing_data_for_step(user, step)
})
return jsonify({'step': None})
@app.route('/api/profiling/consent', methods=['POST'])
def consent():
user_id = request.cookies.get('user_id')
if not user_id:
return jsonify({'error': 'No user_id'}), 400
data = request.json
step_id = data.get('step_id')
granted = data.get('granted')
# Find the step
step = next((s for s in profiling_plan if s['step_id'] == step_id), None)
if not step:
return jsonify({'error': 'Invalid step_id'}), 400
user = get_user(user_id)
if granted:
record_consent(user, step['category'])
log_event('consent_given', {'user_id': user_id, 'step_id': step_id, 'category': step['category'], 'granted': True})
log_event('step_shown', {'user_id': user_id, 'step_id': step_id})
return jsonify({
'step_id': step_id,
'needs_consent': False,
'step': step,
'existing_data': get_existing_data_for_step(user, step)
})
else:
log_event('consent_given', {'user_id': user_id, 'step_id': step_id, 'category': step['category'], 'granted': False})
return jsonify({'message': 'Consent not given'})
@app.route('/api/profiling/step', methods=['POST'])
def submit_step():
user_id = request.cookies.get('user_id')
if not user_id:
return jsonify({'error': 'No user_id'}), 400
data = request.json
step_id = data.get('step_id')
step_data = data.get('data', {})
step = next((s for s in profiling_plan if s['step_id'] == step_id), None)
if not step:
return jsonify({'error': 'Invalid step_id'}), 400
user = get_user(user_id)
if not user_has_consent(user, step['category']):
return jsonify({'error': 'No consent for category'}), 403
# Validate required fields
for field in step['fields']:
if field['required'] and field['name'] not in step_data:
return jsonify({'error': f'Missing required field: {field["name"]}'}), 400
# Store the data
for field in step['fields']:
if field['name'] in step_data:
user.setdefault('data', {})[field['name']] = {
'value': step_data[field['name']],
'category': step['category'],
'collected_at': datetime.utcnow().isoformat()
}
# Mark step as completed
if step_id not in user.get('profiling_steps_completed', []):
user.setdefault('profiling_steps_completed', []).append(step_id)
# Save user (in memory, but in real app, save to DB)
user_profiles[user_id] = user
log_event('step_completed', {'user_id': user_id, 'step_id': step_id})
return jsonify({'success': True})
@app.route('/api/profiling/consent/withdraw', methods=['POST'])
def withdraw_consent():
user_id = request.cookies.get('user_id')
if not user_id:
return jsonify({'error': 'No user_id'}), 400
data = request.json
category = data.get('category')
user = get_user(user_id)
# Update consent
for c in user.get('consent', []):
if c['category'] == category:
c['granted'] = False
# Remove data for this category
data_to_remove = [field for field, d in user.get('data', {}).items() if d['category'] == category]
for field in data_to_remove:
del user['data'][field]
# Remove completed steps that require this category
steps_to_remove = [s['step_id'] for s in profiling_plan if s['category'] == category and s['step_id'] in user.get('profiling_steps_completed', [])]
for step_id in steps_to_remove:
user['profiling_steps_completed'].remove(step_id)
user_profiles[user_id] = user
log_event('consent_withdrawn', {'user_id': user_id, 'category': category})
return jsonify({'success': True})
This is a simple in-memory implementation. In production, we would use a database.
The frontend would handle the cookie and the UI.
This design should meet the requirements.
Let me know if you have any questions or need adjustments.</think>