这是一个稍微复杂的(逐字)示例,用于支持多个跟踪器(例如,将跟踪数据 CC 到客户帐户)、自定义变量和 URL 生成:
更新根:根
super updateRoot: root.
root javascript with: (String streamContents: [:ws | self renderAnalyticsOn: ws]).
renderAnalyticsOn:流
| options |
options := OrderedCollection new.
self trackingConfiguration keysAndValuesDo:
[:tracker :accountid |
| isForClient |
isForClient := tracker notEmpty.
options add: (Array with: tracker , '_setAccount' with: accountid).
isForClient
ifTrue:
[options
add: (Array with: tracker , '_setDomainName' with: 'none');
add: (Array with: tracker , '_setAllowLinker' with: true);
add: (Array with: tracker , '_setAllowHash' with: false)].
self trackingCustomVariables do:
[:array |
array at: 1 put: tracker , array first.
options add: array].
options add: (Array with: tracker , '_trackPageview' with: '/' , self trackingURL)].
stream
nextPutAll: 'var _gaq = _gaq || [];';
nextPutAll: '_gaq.push('.
options do: [:ea | stream json: ea] separatedBy: [stream nextPut: $,].
stream nextPutAll: ');'.
stream
nextPutAll: '(function() {';
nextPutAll: 'var ga = document.createElement(''script''); ga.type = ''text/javascript''; ga.async = true;';
nextPutAll: 'ga.src = (''https:'' == document.location.protocol ? ''https://ssl'' : ''http://www'') + ''.google-analytics.com/ga.js'';';
nextPutAll: 'var s = document.getElementsByTagName(''script'')[0]; s.parentNode.insertBefore(ga, s);';
nextPutAll: '})();'.
跟踪配置
| trackers |
trackers := (Dictionary new)
at: '' put: 'UA-XXXX-YY';
yourself.
self session googleAnalytics ifNotNil: [:v | trackers at: 'b.' put: v].
^trackers.
跟踪自定义变量
^Array with: (Array
with: '_setCustomVar'
with: 1
with: 'Application'
with: self class applicationName
with: 2).
跟踪网址
^String streamContents: [:ws | crumbs do: [:ea | ws nextPutAll: ea title asAnalyticsURL] separatedBy: [ws nextPut: $/]].
asAnalyticsURL
^self asLowercase copyReplace: Character space with: $_