Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

migrate ga.js to analytics.js; complete pageview and event #1

Merged
merged 1 commit into from
Aug 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright © 2013 teambition, http://teambition.com <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@ include in html
<script>
gta = new Gta({
Google: {
account: 'UA-33186961-1',
domain: 'teambition.com'
account: 'UA-33186961-1'
},
Baidu: {
account: '545c20cb01a1afsdfeb0d1f103f99c1'
account: 'ec912ecc405ccd050e4cdf452ef4e85a'
}
});
gta.pageview('/api/hello'); // bind pageview
gta.event('button', 'click', 'other'); // bind events

// track pageview
gta.pageview({ // use single object
'page': '/my-overridden-page?id=1',
'title': 'my overridden page'
})
gta.pageview('/api/hello', '?world'); // use multiple string

// track event
gta.event('button', 'click', 'nav buttons', 4) //@params: category, action, label, value
</script>
```

# Api Documents

* [google](https://developers.google.com/analytics/devguides/collection/analyticsjs/)
* [baidu](http://tongji.baidu.com/open/api/more?p=ref_trackPageview)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gta",
"version": "0.0.1",
"version": "0.0.2",
"main": "lib/index.js",
"ignore": [
"**/.*",
Expand Down
105 changes: 75 additions & 30 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gta",
"version": "0.0.1",
"version": "0.0.2",
"description": "group teambition analysis tool",
"main": "lib/index.js",
"scripts": {
Expand Down
69 changes: 45 additions & 24 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,34 @@ class Gta
@option.account = option.account or ''
@_q = @_initial()

class @Google extends @Base

constructor: (option) ->
super

_initial: ->
unless window.ga?
Gta.appendScript("""
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '#{@option.account}');
ga('send', 'pageview');
""")
return window.ga

pageview: ->
args = for i, val of arguments
val
@_q.push(['_trackPageview', args.join('_')])
return this
data = if typeof args[0] == 'object' then args[0] else args.join('_')
@_q('send', 'pageview', data)

event: ->
args = for i, val of arguments
val
@_q.push(['_trackEvent', args[0], args[1], args[2..].join('_')])

class @Google extends @Base

constructor: (option) ->
super
@_q.push(['_setDomainName', @option.domain]) if @option.domain?

_initial: ->
unless window._gaq?
Gta.appendScript("""
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '#{@option.account}']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
""")
return window._gaq
data = ['send', 'event'].concat(args)
@_q.apply(@_q, data)

class @Baidu extends @Base

Expand All @@ -76,9 +72,34 @@ class Gta
unless window._hmt?
Gta.appendScript("""
var _hmt = _hmt || [];
_hmt.push(['_setAccount', '#{@option.account}']);
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?#{@option.account}";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
""")
return window._hmt

pageview: ->
args = for i, val of arguments
val
if typeof args[0] == 'object'
if args[0]['page']?
data = args[0]['page']
else
data = for i, v of args[0]
v
data = data.join('_')
else
data = args.join('_')
@_q.push(['_trackPageview', data])
return this

event: ->
args = for i, val of arguments
val
data = ['_trackEvent'].concat(args)
@_q.push(data)

exports.Gta = Gta
31 changes: 31 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>homepage</title>
<style>
body {
font-size: 12px;
font-family: Monaco;
}
</style>
<script src="http://sailxjx.local.com/teambition/gta/lib/index.js"></script>
</head>
<body>
hello world
<script>
gta = new Gta({
Google: {
account: 'UA-33186961-1'
},
Baidu: {
account: 'ec912ecc405ccd050e4cdf452ef4e85a'
}
});
gta.pageview({
'page': '/my-overridden-page?id=1',
'title': 'my overridden page'
})
gta.event('button', 'click', 'nav buttons', 4)
</script>
</body>
</html>