Skip to content

feat: collect and track utms at signup #5630

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

Merged
merged 71 commits into from
Jul 7, 2025

Conversation

Zaimwa9
Copy link
Contributor

@Zaimwa9 Zaimwa9 commented Jun 19, 2025

Thanks for submitting a PR! Please check the boxes below:

  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

The point of this PR is to collect (from the webflow redirections ✅ ) the marketing UTMs. They are passed at signup, saved along Hubspot tracker and submitted within the create_lead_form

  • Made hubspot_cookie optional in Hubspot_tracker (could be normalized and a new schema for UTMs)
  • Parse, store and provide UTMs when creating lead form

Webflow
Classic parse and store UTMs params code (settings -> Custom Code -> head code


<script>
  // UTM capture
  // --- CONFIG ---
const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
const STORAGE_KEY = 'flagsmith_utm_data';

// --- CAPTURE UTM PARAMETERS FROM URL ---
function getUTMParamsFromURL() {
  const params = new URLSearchParams(window.location.search);
  console.log(params, window.location)
  const utms = {};
  UTM_KEYS.forEach(key => {
    const val = params.get(key);
    if (val) utms[key] = val;
  });
  return utms;
}

// --- SAVE UTM PARAMS TO LOCALSTORAGE ---
function saveUTMToLocalStorage(utms) {
  if (Object.keys(utms).length > 0) {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(utms));
  }
}

// --- GET UTM DATA FROM LOCALSTORAGE ---
function getUTMFromLocalStorage() {
  try {
    return JSON.parse(localStorage.getItem(STORAGE_KEY)) || {};
  } catch {
    return {};
  }
}

// --- RUN ---
const utmParams = getUTMParamsFromURL();
if (Object.keys(utmParams).length > 0) {
  saveUTMToLocalStorage(utmParams);
}
  
// --- Update app redirect links with UTMs ---
document.addEventListener('DOMContentLoaded', () => {
  const utms = JSON.parse(localStorage.getItem('flagsmith_utm_data') || '{}');
  if (Object.keys(utms).length === 0) return;

  const links = document.querySelectorAll('a[href]');
  const matchUrl = /^https:\/\/app\.flagsmith\.com(\/.*)?$/;

  links.forEach((link) => {
    const href = link.getAttribute('href');
    if (!matchUrl.test(href)) return;

    const url = new URL(href);
    Object.entries(utms).forEach(([k, v]) => url.searchParams.set(k, v));
    link.setAttribute('href', url.toString());
  });
});
</script>

How did you test this code?

  • Tests

Steps to reproduce

  • Activate hubspot lead tracking
  • create a user by coming to http://localhost:8080/signup?utm_campaign=xxx&utm_source=yyy[...]
  • verify in hubspot they have been tracked

image

matthewelwell and others added 30 commits June 3, 2025 11:22
…/flagsmith into fix/hubspot-attribution-issues
Copy link
Member

@khvn26 khvn26 left a comment

Choose a reason for hiding this comment

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

Just a couple comments (one is a reply to @matthewelwell's thread)

@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Jul 2, 2025
@Zaimwa9 Zaimwa9 requested a review from khvn26 July 2, 2025 14:54
Zaimwa9 added 2 commits July 2, 2025 17:00
…agsmith/flagsmith into feat/collect-and-track-utms-at-signup
@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Jul 2, 2025
@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Jul 2, 2025
Copy link
Member

@khvn26 khvn26 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@Zaimwa9 Zaimwa9 merged commit e433c2f into main Jul 7, 2025
29 checks passed
@Zaimwa9 Zaimwa9 deleted the feat/collect-and-track-utms-at-signup branch July 7, 2025 17:43
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 feature New feature or request front-end Issue related to the React Front End Dashboard
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants