Google analytics support for load and subscribe events.
This commit is contained in:
@@ -43,8 +43,9 @@ String.prototype.hashCode = function() {
|
|||||||
* @param {?Cosmopolite.typeCallbacks=} opt_callbacks
|
* @param {?Cosmopolite.typeCallbacks=} opt_callbacks
|
||||||
* @param {?string=} opt_urlPrefix
|
* @param {?string=} opt_urlPrefix
|
||||||
* @param {?string=} opt_namespace
|
* @param {?string=} opt_namespace
|
||||||
|
* @param {?string=} opt_trackingID
|
||||||
*/
|
*/
|
||||||
var Cosmopolite = function(opt_callbacks, opt_urlPrefix, opt_namespace) {
|
var Cosmopolite = function(opt_callbacks, opt_urlPrefix, opt_namespace, opt_trackingID) {
|
||||||
/**
|
/**
|
||||||
* @type {Cosmopolite.typeCallbacks}
|
* @type {Cosmopolite.typeCallbacks}
|
||||||
* @private
|
* @private
|
||||||
@@ -58,7 +59,8 @@ var Cosmopolite = function(opt_callbacks, opt_urlPrefix, opt_namespace) {
|
|||||||
this.urlPrefix_ = opt_urlPrefix || '/cosmopolite';
|
this.urlPrefix_ = opt_urlPrefix || '/cosmopolite';
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
* @private */
|
* @private
|
||||||
|
*/
|
||||||
this.namespace_ = opt_namespace || 'cosmopolite';
|
this.namespace_ = opt_namespace || 'cosmopolite';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,18 +121,45 @@ var Cosmopolite = function(opt_callbacks, opt_urlPrefix, opt_namespace) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @type {Array.<string>} */
|
/** @type {Array.<string>} */
|
||||||
var scriptUrls = [
|
var scriptURLs = [
|
||||||
'/_ah/channel/jsapi'
|
'/_ah/channel/jsapi'
|
||||||
];
|
];
|
||||||
|
if (opt_trackingID) {
|
||||||
|
scriptURLs.push('https://www.google-analytics.com/analytics.js');
|
||||||
|
/**
|
||||||
|
* @type {string}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.analyticsObjName_ = this.uuid_();
|
||||||
|
window['GoogleAnalyticsObject'] = this.analyticsObjName_;
|
||||||
|
var settings = {
|
||||||
|
'storage': 'none',
|
||||||
|
'clientId': localStorage[this.namespace_ + ':tracking_client_id']
|
||||||
|
};
|
||||||
|
var saveCallback = function(analytics) {
|
||||||
|
localStorage[this.namespace_ + ':tracking_client_id'] =
|
||||||
|
analytics.get('clientId');
|
||||||
|
}.bind(this);
|
||||||
|
window[this.analyticsObjName_] = {
|
||||||
|
'l': 1 * new Date(),
|
||||||
|
'q': [
|
||||||
|
['create', opt_trackingID, settings],
|
||||||
|
[saveCallback],
|
||||||
|
['send', 'event', 'cosmopolite', 'load']
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {number}
|
* @type {number}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.numScriptsToLoad_ = scriptUrls.length;
|
this.numScriptsToLoad_ = scriptURLs.length;
|
||||||
scriptUrls.forEach(function(scriptUrl) {
|
scriptURLs.forEach(function(scriptURL) {
|
||||||
/** @type {Node} */
|
/** @type {Node} */
|
||||||
var script = document.createElement('script');
|
var script = document.createElement('script');
|
||||||
script.src = scriptUrl;
|
script.src = scriptURL;
|
||||||
|
script.async = true;
|
||||||
script.onload = this.onLoad_.bind(this);
|
script.onload = this.onLoad_.bind(this);
|
||||||
document.body.appendChild(script);
|
document.body.appendChild(script);
|
||||||
}, this);
|
}, this);
|
||||||
@@ -285,17 +314,22 @@ Cosmopolite.prototype.subscribe = function(subject, opt_messages, opt_last_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.sendRPC_('subscribe', args, function(response) {
|
this.sendRPC_('subscribe', args, function(response) {
|
||||||
|
/** @type {string} */
|
||||||
|
var result = response['result'];
|
||||||
|
if (result == 'ok') {
|
||||||
// unsubscribe may have been called since we sent the RPC. That's racy
|
// unsubscribe may have been called since we sent the RPC. That's racy
|
||||||
// without waiting for the promise, but do our best
|
// without waiting for the promise, but do our best
|
||||||
if (subjectString in this.subscriptions_) {
|
if (subjectString in this.subscriptions_) {
|
||||||
this.subscriptions_[subjectString].state =
|
this.subscriptions_[subjectString].state =
|
||||||
Cosmopolite.SubscriptionState_.ACTIVE;
|
Cosmopolite.SubscriptionState_.ACTIVE;
|
||||||
}
|
}
|
||||||
/** @type {string} */
|
|
||||||
var result = response['result'];
|
|
||||||
if (result == 'ok') {
|
|
||||||
resolve();
|
resolve();
|
||||||
|
if (this.analyticsObj_) {
|
||||||
|
this.analyticsObj_(
|
||||||
|
'send', 'event', 'cosmopolite', 'subscribe', subjectString);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
delete this.subscriptions_[subjectString];
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -577,6 +611,14 @@ Cosmopolite.prototype.onLoad_ = function() {
|
|||||||
// Shutdown during startup
|
// Shutdown during startup
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (this.analyticsObjName_) {
|
||||||
|
/**
|
||||||
|
* @type {Object}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.analyticsObj_ = window[this.analyticsObjName_];
|
||||||
|
delete window[this.analyticsObjName_];
|
||||||
|
}
|
||||||
this.registerMessageHandlers_();
|
this.registerMessageHandlers_();
|
||||||
this.createChannel_();
|
this.createChannel_();
|
||||||
};
|
};
|
||||||
@@ -1183,6 +1225,9 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
if (e['profile']) {
|
if (e['profile']) {
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
this.profile_ = e['profile'];
|
this.profile_ = e['profile'];
|
||||||
|
if (this.analyticsObj_) {
|
||||||
|
this.analyticsObj_('set', 'userId', this.profile_);
|
||||||
|
}
|
||||||
this.profilePromises_.forEach(function(resolve) {
|
this.profilePromises_.forEach(function(resolve) {
|
||||||
resolve(this.profile_);
|
resolve(this.profile_);
|
||||||
}, this);
|
}, this);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ var onReady = function() {
|
|||||||
'onPin': onPin,
|
'onPin': onPin,
|
||||||
'onUnpin': onUnpin,
|
'onUnpin': onUnpin,
|
||||||
}
|
}
|
||||||
cosmo = new Cosmopolite(callbacks);
|
cosmo = new Cosmopolite(callbacks, null, null, 'UA-37845853-3');
|
||||||
|
|
||||||
elements['messageText'].addEventListener('keypress', messageKeyPress);
|
elements['messageText'].addEventListener('keypress', messageKeyPress);
|
||||||
elements['pinText'].addEventListener('keypress', pinKeyPress);
|
elements['pinText'].addEventListener('keypress', pinKeyPress);
|
||||||
|
|||||||
Reference in New Issue
Block a user