PageSourceSearch

https://www.khokirin.com/modules/flexi/js/ga_social_tracking/ga_social_tracking.js

js khokirin.com collected 2026-10-09 17:02:23 UTC 4,488 bytes, 138 lines download raw bytes

1// Copyright 2011 Google Inc. All Rights Reserved.
2
3/**
4 * @fileoverview A simple script to automatically track Facebook and Twitter
5 * buttons using Google Analytics social tracking feature.
6 * @author [email protected] (Nick Mihailovski)
7 */
8
9
10/**
11 * Namespace.
12 * @type {Object}.
13 */
14var _ga = _ga || {};
15
16
17/**
18 * Ensure global _gaq Google Anlaytics queue has be initialized.
19 * @type {Array}
20 */
21var _gaq = _gaq || [];
22
23
24/**
25 * Helper method to track social features. This assumes all the social
26 * scripts / apis are loaded synchronously. If they are loaded async,
27 * you might need to add the nextwork specific tracking call to the
28 * a callback once the network's script has loaded.
29 * @param {string} opt_pageUrl An optional URL to associate the social
30 *     tracking with a particular page.
31 * @param {string} opt_trackerName An optional name for the tracker object.
32 */
33_ga.trackSocial = function(opt_pageUrl, opt_trackerName) {
34  _ga.trackFacebook(opt_pageUrl, opt_trackerName);
35  _ga.trackTwitter(opt_pageUrl, opt_trackerName);
36};
37
38
39/**
40 * Tracks Facebook likes, unlikes and sends by suscribing to the Facebook
41 * JSAPI event model. Note: This will not track facebook buttons using the
42 * iFrame method.
43 * @param {string} opt_pageUrl An optional URL to associate the social
44 *     tracking with a particular page.
45 * @param {string} opt_trackerName An optional name for the tracker object.
46 */
47_ga.trackFacebook = function(opt_pageUrl, opt_trackerName) {
48  var trackerName = _ga.buildTrackerName_(opt_trackerName);
49  try {
50    if (FB && FB.Event && FB.Event.subscribe) {
51      FB.Event.subscribe('edge.create', function(targetUrl) {
52        _gaq.push([trackerName + '_trackSocial', 'facebook', 'like',
53            targetUrl, opt_pageUrl]);
54      });
55      FB.Event.subscribe('edge.remove', function(targetUrl) {
56        _gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike',
57            targetUrl, opt_pageUrl]);
58      });
59      FB.Event.subscribe('message.send', function(targetUrl) {
60        _gaq.push([trackerName + '_trackSocial', 'facebook', 'send',
61            targetUrl, opt_pageUrl]);
62      });
63    }
64  } catch (e) {}
65};
66
67
68/**
69 * Returns the normalized tracker name configuration parameter.
70 * @param {string} opt_trackerName An optional name for the tracker object.
71 * @return {string} If opt_trackerName is set, then the value appended with
72 *     a . Otherwise an empty string.
73 * @private
74 */
75_ga.buildTrackerName_ = function(opt_trackerName) {
76  return opt_trackerName ? opt_trackerName + '.' : '';
77};
78
79
80/**
81 * Tracks everytime a user clicks on a tweet button from Twitter.
82 * This subscribes to the Twitter JS API event mechanism to listen for
83 * clicks coming from this page. Details here:
84 * http://dev.twitter.com/pages/intents-events#click
85 * This method should be called once the twitter API has loaded.
86 * @param {string} opt_pageUrl An optional URL to associate the social
87 *     tracking with a particular page.
88 * @param {string} opt_trackerName An optional name for the tracker object.
89 */
90_ga.trackTwitter = function(opt_pageUrl, opt_trackerName) {
91  var trackerName = _ga.buildTrackerName_(opt_trackerName);
92  try {
93    if (twttr && twttr.events && twttr.events.bind) {
94      twttr.events.bind('tweet', function(event) {
95        if (event) {
96          var targetUrl; // Default value is undefined.
97          if (event.target && event.target.nodeName == 'IFRAME') {
98            targetUrl = _ga.extractParamFromUri_(event.target.src, 'url');
99          }
100          _gaq.push([trackerName + '_trackSocial', 'twitter', 'tweet',
101            targetUrl, opt_pageUrl]);
102        }
103      });
104    }
105  } catch (e) {}
106};
107
108
109/**
110 * Extracts a query parameter value from a URI.
111 * @param {string} uri The URI from which to extract the parameter.
112 * @param {string} paramName The name of the query paramater to extract.
113 * @return {string} The un-encoded value of the query paramater. underfined
114 *     if there is no URI parameter.
115 * @private
116 */
117_ga.extractParamFromUri_ = function(uri, paramName) {
118  if (!uri) {
119    return;
120  }
121  var uri = uri.split('#')[0];  // Remove anchor.
122  var parts = uri.split('?');  // Check for query params.
123  if (parts.length == 1) {
124    return;
125  }
126  var query = decodeURI(parts[1]);
127
128  // Find url param.
129  paramName += '=';
130  var params = query.split('&');
131  for (var i = 0, param; param = params[i]; ++i) {
132    if (param.indexOf(paramName) === 0) {
133      return unescape(param.split('=')[1]);
134    }
135  }
136  return;
137};
138

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.