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

Implement user_id and pass it in marketo submission #14440

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 35 additions & 0 deletions static/js/src/cookie-policy-with-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Set a unique user_id if the user has accepted cookies.
If the user has not set their cookie preference, wait for the cookie policy to run first.
*/

import { v4 as uuidv4 } from "https://jspm.dev/uuid";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to install and import uuidv4 npm package instead. This import looks a bit unusual to me. Is it written somewhere that this is the way it should be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't be available client side without a bundler, so I've used a cdn as suggested here


const getCookie = (targetCookie) =>
document.cookie.match(new RegExp("(^| )" + targetCookie + "=([^;]+)"));
let cookieAcceptanceValue = getCookie("_cookies_accepted");

if (cookieAcceptanceValue === null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the value is None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The value of that cookie when not set is null, but updated to catch more falsey values

cpNs.cookiePolicy(setUserId);
} else {
setUserId();
}

function setUserId() {
cookieAcceptanceValue = getCookie("_cookies_accepted");
if (
cookieAcceptanceValue?.[2] === "all" ||
cookieAcceptanceValue?.[2] === "performance"
) {
// Check if user doesn't already have a user_id
if (!getCookie("user_id")) {
// Generate a universally unique identifier
const user_id = uuidv4();
document.cookie = "user_id=" + user_id + ";max-age=31536000;";

dataLayer.push({
user_id: user_id,
});
}
}
}
4 changes: 1 addition & 3 deletions templates/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
{% block content_experiment %}{% endblock %}
<!-- Cookie policy -->
<script src="{{ versioned_static('js/dist/cookie-policy.js') }}"></script>
<script>
cpNs.cookiePolicy();
</script>
<script type="module" src="{{ versioned_static('js/src/cookie-policy-with-callback.js') }}"></script>
<script src="https://assets.ubuntu.com/v1/703e23c9-lazysizes+noscript+native-loading.5.1.2.min.js"
defer></script>

Expand Down
4 changes: 4 additions & 0 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,10 @@ def marketo_submit():
enrichment_fields["country"] = form_fields["country"]
form_fields.pop("country")

user_id = flask.request.cookies.get("user_id")
if user_id:
enrichment_fields["Google_Analytics_User_ID__c"] = user_id

payload = {
"formId": form_fields.pop("formid"),
"input": [
Expand Down
Loading