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

fix: Relax Hubspot cookie tracking #4905

Merged
merged 6 commits into from
Dec 10, 2024
Merged

Conversation

zachaysan
Copy link
Contributor

@zachaysan zachaysan commented Dec 9, 2024

Changes

Fixes #4900

Since Hubspot cookies are used to merge identities, it is preferable to ignore a Hubspot token for different users on the same machine.

How did you test this code?

Proved that the error was raised without it then wrote a test to verify that multiple users can supply the same Hubspot cookie without an error being raised.

@zachaysan zachaysan requested a review from a team as a code owner December 9, 2024 15:58
@zachaysan zachaysan requested review from matthewelwell and removed request for a team December 9, 2024 15:58
Copy link

vercel bot commented Dec 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) Visit Preview Dec 10, 2024 2:01pm
flagsmith-frontend-preview ⬜️ Ignored (Inspect) Visit Preview Dec 10, 2024 2:01pm
flagsmith-frontend-staging ⬜️ Ignored (Inspect) Visit Preview Dec 10, 2024 2:01pm

@github-actions github-actions bot added api Issue related to the REST API fix labels Dec 9, 2024
Copy link
Contributor

github-actions bot commented Dec 9, 2024

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-api-test:pr-4905 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-e2e:pr-4905 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-4905 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-4905 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-4905 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-4905 Finished ✅ Results

Copy link
Contributor

github-actions bot commented Dec 9, 2024

Uffizzi Preview deployment-58955 was deleted.

Comment on lines 21 to 35
try:
HubspotTracker.objects.update_or_create(
user=request.user,
defaults={
"hubspot_cookie": hubspot_cookie,
},
)
logger.info(
f"Created HubspotTracker instance for user {request.user.email} with cookie {hubspot_cookie}"
)
except IntegrityError:
logger.info(
f"HubspotTracker could not be created for user {request.user.email}"
f" due to cookie conflict with cookie {hubspot_cookie}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I'm missing something here as to why we're using update_or_create here, but I don't love the implementation - mostly because IntegrityError might not always indicate what we're assuming here.

I think we should just be more explicit, and do something like:

if HubspotTracker.objects.filter(hubspot_cookie=hubspot_cookie).exists():
    logger.info(
        f"HubspotTracker could not be created for user {request.user.email}"
        f" due to cookie conflict with cookie {hubspot_cookie}"
    )
else:
    HubspotTracker.objects.create(...)
    logger.info(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've switched to a filter and exclude for the check. I'm keeping the update_or_create here because if a users hubspot cookie is changed we want to include that change in their tracking data.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.39%. Comparing base (f97c56f) to head (2380431).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4905   +/-   ##
=======================================
  Coverage   97.38%   97.39%           
=======================================
  Files        1189     1189           
  Lines       41453    41468   +15     
=======================================
+ Hits        40371    40386   +15     
  Misses       1082     1082           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

.exists()
):
logger.info(
f"HubspotTracker could not be created for user {request.user.email}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'd actually rather that we just log the user id here (and below) - I don't like the idea of logging emails if we can avoid it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's no problem. I've switched the loggers to use user ids.

@github-actions github-actions bot added fix and removed fix labels Dec 10, 2024
@zachaysan zachaysan merged commit 4f20e5a into main Dec 10, 2024
35 checks passed
@zachaysan zachaysan deleted the fix/relax_hubspot_cookie_tracking branch December 10, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hubspot cookie data handled incorrectly for user that already has one
2 participants