Skip to content

Instantly share code, notes, and snippets.

@lucemia
Created December 3, 2013 12:41
Show Gist options
  • Select an option

  • Save lucemia/7768545 to your computer and use it in GitHub Desktop.

Select an option

Save lucemia/7768545 to your computer and use it in GitHub Desktop.
action pattern
patterns = {
0 :[
# Common Pattern
# click ad behavior
(r'utm_source', r'ad'),
(r'utm_source=([\w]+)', r'ad:\1'),
],
28: [
# Mayuki Pattern
(r'mayuki', r'visit'),
(r'showProduct', r'product'),
(r'CrShopCar', r'cart'),
(r'CrShopCar03', r'payment'),
]
}
import re
def parse_actions(advertiser_id, url):
actions = []
ps = patterns.get(advertiser_id, []) + patterns[0]
for pattern, repl in ps:
if re.findall(pattern, url):
actions.append(re.sub(pattern, repl, url))
return actions
assert parse_actions(28, 'http://www.mayuki.com.tw') == ["visit"]
assert parse_actions(28, 'www.mayuki.com.tw/showProduct=123') == ["visit", "product"]
assert parse_actions(28, 'www.mayuki.com.tw/?utm_source=yahoo') == ["ad", "ad:yahoo"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment