Skip to content

Commit

Permalink
Merge pull request #13244 from pedoch/WD-6294-modify-jp-ubuntu-com-cn…
Browse files Browse the repository at this point in the history
…-ubuntu-com-acquisition-url-to-respect-length-limit

fix: shorten form acquisition URLs - WD-6294
  • Loading branch information
pedoch authored Nov 2, 2023
2 parents d45d18b + 068368e commit 3ab727d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
75 changes: 75 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Packages
from unittest import TestCase

# Local
from webapp.views import shorten_acquisition_url


class TestViewsFunctions(TestCase):
def test_shorten_acquisition_url(self):
"""
Test that the acquisition url is shortened to < 255 characters
fcclid and gclid parameters are removed initially
If the url is still too long, all parameters are removed
"""

# this url should still have UTM parameters after shortening
first_url = (
"https://ubuntu.com/engage/secure-kubernetes-at-the-edge?"
"utm_source=facebook_ad&utm_medium=cpc"
"&utm_campaign=7018e000000Lrr2AAC"
"&fbclid=IwAR15h71YCzbtXmItLpTWGKvX6LGlbBNrsYAsoq-IEJiF"
"9SYy_aDXgpsIqn0_aem_AbSMY4GVnRtIgih_p0mTaKvNZH6T3Qy73p"
"lm8KSqnZJXAsol_n1J440OjNsdU2s6-a0urWDolTPSE0nv3SYoY3jU"
"&gclid=IwAR15h71YCzbtXmItLpTWGKvX6LGlbBNrsYAsoq-IEJiF9"
"SYy_aDXgpsIqn0_aem_AbSMY4GVnRtIgih_p0mTaKvNZH6T3Qy73p"
"lm8KSqnZJXAsol_n1J440OjNsdU2s6-a0urWDolTPSE0nv3SYoY3jU"
)

first_url_check = (
"https://ubuntu.com/engage/secure-kubernetes-at-the-edge?"
"utm_source=facebook_ad&utm_medium=cpc"
"&utm_campaign=7018e000000Lrr2AAC"
)

# this url should not have UTM parameters after shortening
second_url = (
"https://ubuntu.com/engage/secure-kubernetes-at-the-edge?"
"utm_source=facebook_ad&utm_medium=cpcrHds-f323rdf34v4t24fd"
"fsfrdfSAsewetv_trfsdgdfg4234rchb534z2243h_rtgrcawretthfghff"
"&utm_campaign=7018ihtrHdsf323rrdfSASYHe000000Lr8450oh2oEEDV"
"EfdfodifnjvFSF0w94ngoinsdgfw4c12r2AAC"
"&fbclid=IwAR15h71YCzbtXmItLpTWGKvX6LGlbBNrsYAsoq-IEJiF9SYy_aDX"
"gpsIqn0_aem_AbSMY4GVnRtIgih_p0mTaKvNZH6T3Qy73plm8KSqnZJXAsol_n"
"1J440OjNsdU2s6-a0urWDolTPSE0nv3SYoY3jU"
"&gclid=IwAR15h71YCzbtXmItLpTWGKvX6LGlbBNrsYAsoq-IEJiF9SYy_aDXg"
"psIqn0_aem_AbSMY4GVnRtIgih_p0mTaKvNZH6T3Qy73plm8KSqnZJXAsol_n1"
"J440OjNsdU2s6-a0urWDolTPSE0nv3SYoY3jU"
)

second_url_check = (
"https://ubuntu.com/engage/secure-kubernetes-at-the-edge"
)

# this url should not be shortened
third_url = (
"https://ubuntu.com/engage/secure-kubernetes-at-the-edge?"
"utm_source=facebook_ad&utm_medium=button"
"&utm_campaign=7018jyr44t534e000000Lrr2AAC"
)

# Case 1: url is shorten to less than 255 characters
# and only fbclid/gclid parameters are removed
self.assertEqual(shorten_acquisition_url(first_url), first_url_check)
self.assertLess(len(shorten_acquisition_url(first_url)), 255)

# Case 2: url is shorten to less than 255 characters
# and all parameters are removed (including UTM parameters)
self.assertEqual(shorten_acquisition_url(second_url), second_url_check)
self.assertLess(len(shorten_acquisition_url(second_url)), 255)

# Case 3: url is already less than 255 characters
# and is not shortened
self.assertEqual(shorten_acquisition_url(third_url), third_url)
self.assertLess(len(shorten_acquisition_url(third_url)), 255)
38 changes: 36 additions & 2 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,37 @@ def sitemap_index():
return response


def shorten_acquisition_url(acquisition_url):
"""
Shorten the acquisition URL sent when submitting forms
"""

if len(acquisition_url) > 255:
url_without_params = acquisition_url.split("?")[0]
url_params_string = acquisition_url.split("?")[1]
url_params_list = url_params_string.split("&")
url_params_to_remove = []

# Check for and remove fbclid and gclid parameters
for param in url_params_list:
if param.startswith("fbclid") or param.startswith("gclid"):
url_params_to_remove.append(param)

for param in url_params_to_remove:
url_params_list.remove(param)

new_acquisition_url = (
url_without_params + "?" + "&".join(url_params_list)
)

# If the URL is still too long, remove all parameters
if len(new_acquisition_url) > 255:
return new_acquisition_url.split("?")[0]

return new_acquisition_url
return acquisition_url


def marketo_submit():
form_fields = {}
for key in flask.request.form:
Expand Down Expand Up @@ -895,9 +926,12 @@ def marketo_submit():
}

if "acquisition_url" in form_fields:
enrichment_fields["acquisition_url"] = form_fields["acquisition_url"]
shortened_url = shorten_acquisition_url(form_fields["acquisition_url"])
form_fields["acquisition_url"] = shortened_url
enrichment_fields["acquisition_url"] = shortened_url
else:
enrichment_fields["acquisition_url"] = referrer
shortened_url = shorten_acquisition_url(referrer)
enrichment_fields["acquisition_url"] = shortened_url

if "preferredLanguage" in form_fields:
enrichment_fields["preferredLanguage"] = form_fields[
Expand Down

0 comments on commit 3ab727d

Please sign in to comment.