Created
December 13, 2011 04:01
-
-
Save yosimox/1470510 to your computer and use it in GitHub Desktop.
カフェスクリプトで外部リンクトラッキング用のテスト
This file contains 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
gaConf = { | |
linkFilter : { | |
#外部サイトへのリンクトラック true or false | |
offsite : true, | |
#除外サイト | |
excludeLink : [ | |
/google/, | |
/livedoor/ | |
], | |
internalTrack : { | |
"pdf" : /\.pdf$/, | |
"zip" : /\.zip$/, | |
"ppt" : /\.ppt$/, | |
"doc" : /\.doc$/, | |
"mail" : /^mailto\:/ | |
} | |
} | |
} | |
class EventCoffee | |
constructor : (filter) -> | |
@filter = filter | |
getEvent : (arg, arg2) -> | |
console.log("arg: #{arg}") | |
console.log("arg2: #{arg2}") | |
alert(arg.href) | |
observe : (element, eventName, handler)-> | |
false if element? | |
if(element.addEventListener) | |
element.addEventListener(eventName, handler, false) | |
else if(element.attachEvent) | |
element.attachEvent('on'+eventName, handler); | |
else | |
false | |
binding : (binded, func, arg)-> | |
()->func.apply(binded, arg) | |
filterLink : (hrefUrl)-> | |
res = [] | |
hostname = new RegExp("^" + location.host) | |
domUrl = hrefUrl.replace(/http(s)?:\/\//, "") | |
#内部リンク | |
if(domUrl.match(hostname)) | |
for key,val of @filter.internalTrack | |
if(domUrl.match(val)) | |
res.push(true, key) | |
return res | |
res.push(false) | |
return res | |
else | |
#外部リンク | |
for val in @filter.excludeLink | |
if(domUrl.match(val)) | |
res.push(false) | |
return res | |
res.push(true, "outboundLink") | |
return res | |
return res | |
linkListener : (tagName='a', func=@getEvent)-> | |
for i in document.getElementsByTagName(tagName) | |
res = @filterLink(i.href) | |
@observe(i, "click", @binding(i, func, [i, res])) if res[0]==true | |
false | |
window.eventCoffee = new EventCoffee(gaConf.linkFilter) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment