-
-
Save geekwolverine/cae53137872c91e1ec9ac70034ce36a2 to your computer and use it in GitHub Desktop.
Clean utm parameters on link.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import urlparse | |
def linky(url): | |
"""Sanitize link. clean utm parameters on link.""" | |
if not re.match(r'^https?:\/\/', url): | |
url = 'http://%s' % url | |
rv = urlparse.urlparse(url) | |
if rv.query: | |
query = re.sub(r'utm_\w+=[^&]+&?', '', rv.query) | |
url = '%s://%s%s?%s' % (rv.scheme, rv.hostname, rv.path, query) | |
# remove ? at the end of url | |
url = re.sub(r'\?$', '', url) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment