asai@blixus.co.jp | My favorites | Profile | Sign out
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2011 Google Inc. All Rights Reserved.

/**
* @fileoverview A simple script to automatically track Facebook and Twitter
* buttons using Google Analytics social tracking feature.
* @author api.nickm@google.com (Nick Mihailovski)
*/


/**
* Namespace.
* @type {Object}.
*/
var _ga = _ga || {};


/**
* Ensure global _gaq Google Anlaytics queue has be initialized.
* @type {Array}
*/
var _gaq = _gaq || [];


/**
* Helper method to track social features. This assumes all the social
* scripts / apis are loaded synchronously. If they are loaded async,
* you might need to add the nextwork specific tracking call to the
* a callback once the network's script has loaded.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackSocial = function(opt_pageUrl, opt_trackerName) {
_ga.trackFacebook(opt_pageUrl, opt_trackerName);
_ga.trackTwitter(opt_pageUrl, opt_trackerName);
};


/**
* Tracks Facebook likes, unlikes and sends by suscribing to the Facebook
* JSAPI event model. Note: This will not track facebook buttons using the
* iFrame method.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackFacebook = function(opt_pageUrl, opt_trackerName) {
var trackerName = _ga.buildTrackerName_(opt_trackerName);
try {
if (FB && FB.Event && FB.Event.subscribe) {
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'like',
targetUrl, opt_pageUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike',
targetUrl, opt_pageUrl]);
});
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push([trackerName + '_trackSocial', 'facebook', 'send',
targetUrl, opt_pageUrl]);
});
}
} catch (e) {}
};


/**
* Returns the normalized tracker name configuration parameter.
* @param {string} opt_trackerName An optional name for the tracker object.
* @return {string} If opt_trackerName is set, then the value appended with
* a . Otherwise an empty string.
* @private
*/
_ga.buildTrackerName_ = function(opt_trackerName) {
return opt_trackerName ? opt_trackerName + '.' : '';
};


/**
* Tracks everytime a user clicks on a tweet button from Twitter.
* This subscribes to the Twitter JS API event mechanism to listen for
* clicks coming from this page. Details here:
* http://dev.twitter.com/pages/intents-events#click
* This method should be called once the twitter API has loaded.
* @param {string} opt_pageUrl An optional URL to associate the social
* tracking with a particular page.
* @param {string} opt_trackerName An optional name for the tracker object.
*/
_ga.trackTwitter = function(opt_pageUrl, opt_trackerName) {
var trackerName = _ga.buildTrackerName_(opt_trackerName);
try {
if (twttr && twttr.events && twttr.events.bind) {
twttr.events.bind('tweet', function(event) {
if (event) {
var targetUrl; // Default value is undefined.
if (event.target && event.target.nodeName == 'IFRAME') {
targetUrl = _ga.extractParamFromUri_(event.target.src, 'url');
}
_gaq.push([trackerName + '_trackSocial', 'twitter', 'tweet',
targetUrl, opt_pageUrl]);
}
});
}
} catch (e) {}
};


/**
* Extracts a query parameter value from a URI.
* @param {string} uri The URI from which to extract the parameter.
* @param {string} paramName The name of the query paramater to extract.
* @return {string} The un-encoded value of the query paramater. underfined
* if there is no URI parameter.
* @private
*/
_ga.extractParamFromUri_ = function(uri, paramName) {
if (!uri) {
return;
}
var uri = uri.split('#')[0]; // Remove anchor.
var parts = uri.split('?'); // Check for query params.
if (parts.length == 1) {
return;
}
var query = decodeURI(parts[1]);

// Find url param.
paramName += '=';
var params = query.split('&');
for (var i = 0, param; param = params[i]; ++i) {
if (param.indexOf(paramName) === 0) {
return unescape(param.split('=')[1]);
}
}
return;
};

Change log

r3 by pr...@google.com on Jun 29, 2011   Diff
checking in social tracking sample and
demos
Go to: 

Older revisions

All revisions of this file

File info

Size: 4488 bytes, 138 lines

File properties

svn:mime-type
text/javascript
svn:executable
*
Powered by Google Project Hosting