Skip to content

Commit

Permalink
Merge pull request #14440 from mtruj013/marketo-user
Browse files Browse the repository at this point in the history
Implement user_id and pass it in marketo submission
  • Loading branch information
akbarkz authored Oct 31, 2024
2 parents ec5bcc3 + 7b8f854 commit af116b9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
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";

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

if (!cookieAcceptanceValue) {
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,
});
}
}
}
10 changes: 9 additions & 1 deletion templates/templates/_tag_manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
'pages.ubuntu.com', '/tutorials', 'docs.ubuntu.com']);
</script>
<!-- End Google Analytics and Google Optimize -->

<script>
const getCookie = () => document.cookie.match(new RegExp("(^| )" + "user_id" + "=([^;]+)"));
let idValue = getCookie()[2];
if (idValue) {
dataLayer.push({
user_id: idValue,
});
}
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
Expand Down
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 @@ -978,6 +978,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

0 comments on commit af116b9

Please sign in to comment.