Skip to content

Conversation

@mocca102
Copy link
Contributor

Add Support for UTM parameters

Where?

Appended to the origin_referrer parameter in tracker.js module

What are the supported parameters

  • utm_source
  • utm_campain
  • utm_medium

Tests

  • Added validateOriginReferrer
  • Validates origin_referrer is appended with UTM parameters for all tracking events

@mocca102 mocca102 requested a review from a team April 22, 2025 21:31
Comment on lines 71 to 91
const utmSource = searchParams.get('utm_source');
const utmMedium = searchParams.get('utm_medium');
const utmCampaign = searchParams.get('utm_campaign');

// Add UTM parameters to origin_referrer if they exist
if (utmSource || utmMedium || utmCampaign) {
aggregateParams.origin_referrer += '?';

if (utmSource) {
aggregateParams.origin_referrer += `utm_source=${utmSource}`;
}

if (utmMedium) {
if (utmSource) aggregateParams.origin_referrer += '&';
aggregateParams.origin_referrer += `utm_medium=${utmMedium}`;
}

if (utmCampaign) {
if (utmSource || utmMedium) aggregateParams.origin_referrer += '&';
aggregateParams.origin_referrer += `utm_campaign=${utmCampaign}`;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might I suggest an alternative?

    const utmParamKeys = ['utm_source', 'utm_medium', 'utm_campaign'];
    const utmQueryParamStrArr = [];

    utmParamKeys.forEach((key) => {
      const utmParamValue = searchParams.get(key);

      if (utmParamValue) {
        utmQueryParamStrArr.push(`${key}=${utmParamValue}`);
      }
    });

    if (utmQueryParamStrArr.length) {
      aggregateParams.origin_referrer += `?${utmQueryParamStrArr.join('&')}`
    }

Not to nitpick on implementation, but something like this would make it easier if we needed to add in more utm fields in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that

@mocca102 mocca102 requested a review from Mudaafi April 24, 2025 14:46
Copy link
Contributor

@Mudaafi Mudaafi left a comment

Choose a reason for hiding this comment

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

LGTM! I like the way you wrote the tests, made this PR easier to review. Let's get a @jjl014 or @esezen to review too since they're the shepherds

@Mudaafi Mudaafi requested review from esezen and jjl014 April 25, 2025 04:17
esezen
esezen previously approved these changes Apr 25, 2025
Copy link
Contributor

@esezen esezen left a comment

Choose a reason for hiding this comment

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

LGTM!

@esezen esezen dismissed their stale review April 25, 2025 08:34

Saw a broken test. I'll look into it

Copy link
Contributor

@esezen esezen left a comment

Choose a reason for hiding this comment

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

LGTM!
@mocca102 let me know what do you think about 9f4be40

mocca102 and others added 2 commits April 26, 2025 02:18
Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com>
Copy link
Contributor

@esezen esezen left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@jjl014 jjl014 left a comment

Choose a reason for hiding this comment

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

LGTM! 🔥

const utmParameters = 'utm_source=attentive&utm_medium=sms&utm_campaign=campaign_1';
const url = `http://localhost.test/path/name?query=term&category=cat&${utmParameters}`;

function validateOriginReferrer(requestParams) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oooo, I dig this!

@esezen esezen merged commit 45bb9bc into master Apr 29, 2025
8 checks passed
@esezen esezen deleted the ci-4382-offsite-tracking-add-source-medium-and-campaign-to-utm branch April 29, 2025 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants