From 3feccdd527b7ca051bd6d6e23baf545c3be7e0af Mon Sep 17 00:00:00 2001 From: eliasdabbas Date: Tue, 16 Jan 2018 13:36:10 +0400 Subject: [PATCH] rename url function and file --- advertools/ad_from_string.py | 2 +- advertools/url_builders.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 advertools/url_builders.py diff --git a/advertools/ad_from_string.py b/advertools/ad_from_string.py index d9186f53..66efffd0 100644 --- a/advertools/ad_from_string.py +++ b/advertools/ad_from_string.py @@ -56,4 +56,4 @@ def ad_from_string(s, capitalize=True): remain = ' '.join(str_words[counter:]) text_ad.append(remain) - return [string.capwords(x) if capitalize else x for x in text_ad] \ No newline at end of file + return [string.capwords(x) if capitalize else x for x in text_ad] diff --git a/advertools/url_builders.py b/advertools/url_builders.py new file mode 100644 index 00000000..5134710d --- /dev/null +++ b/advertools/url_builders.py @@ -0,0 +1,32 @@ +import urllib + + +def url_utm_ga(url, utm_source, utm_medium=None, utm_campaign=None, + utm_content=None, utm_term=None): + """Generate a utm-encoded URL for your campaigns. + + URLs are are not validated. All parameters are URL-encoded. + + Parameters + ---------- + url : a valid URL, required + utm_source : the referrer of the traffic (e.g. facebook, twitter), required + utm_medium : marketing medium (e.g. banner, email) + utm_campaign : the name of the campaign (e.g. summer_promo, 20pct_off) + utm_content : ad name / differentiator (e.g. 728x90, mpu, square_banner) + utm_term : search terms bid on (only relevant for search campaigns) + + Examples + -------- + >>> url_utm_ga('mysite.com', utm_source='the source) + ... 'mysite.com?utm_source=the+source' + + >>> url_utm_ga('mysite.com', utm_source='the source', + utm_medium='THE MEDIUM!!', utm_campaign='campaign*name&^%', + utm_content='728x90') + ... 'mysite.com?utm_content=728x90&utm_campaign=campaign%2Aname%26%5E%25&utm_medium=THE+MEDIUM%21%21&utm_source=the+source' + + """ + url += '?' + params = {k: v for k, v in locals().items() if k != 'url'} + return url + urllib.parse.urlencode({k: v for k, v in params.items() if v})