Stackedit/res/extensions/googleAnalytics.js

218 lines
6.0 KiB
JavaScript
Raw Permalink Normal View History

2013-06-10 21:22:32 +00:00
define([
"jquery",
"underscore",
2013-06-16 18:29:54 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"settings",
"config",
2013-06-22 23:48:57 +00:00
], function($, _, utils, Extension, settings) {
2013-06-10 21:22:32 +00:00
2013-06-22 23:48:57 +00:00
var googleAnalytics = new Extension("googleAnalytics", "Google Analytics", true);
googleAnalytics.settingsBlock = '<p>Sends anonymous statistics about usage and errors to help improve StackEdit.</p>';
2013-06-10 21:22:32 +00:00
var isLoaded = false;
var isOffline = false;
window["_gaq"] = [];
var init = function() {
if(isLoaded === false && isOffline === false) {
var gaUrl = "/ga.js";
if(location.search.match(/(\?|&)console/)) {
gaUrl = "/u/ga_debug.js";
}
$.ajax({
url: "http://www.google-analytics.com" + gaUrl,
dataType: "script"
}).done(function() {
isLoaded = true;
});
}
};
2013-06-16 18:29:54 +00:00
var lastPageView = 0;
function trackPageView() {
if(utils.currentTime - lastPageView > 180000) {
_gaq.push([
'_trackPageview'
]);
lastPageView = utils.currentTime;
}
}
googleAnalytics.onPeriodicRun = function() {
trackPageView();
};
2013-06-12 00:00:12 +00:00
googleAnalytics.onReady = function() {
// First configure GA
_gaq.push([
'_setAccount',
GOOGLE_ANALYTICS_ACCOUNT_ID
]);
2013-06-16 18:29:54 +00:00
trackPageView();
2013-06-12 00:00:12 +00:00
// Collect informations about user settings
_gaq.push([
'_trackEvent',
"Settings",
'layoutOrientation',
"" + settings.layoutOrientation
]);
_gaq.push([
'_trackEvent',
"Settings",
'lazyRendering',
"" + settings.lazyRendering
]);
_gaq.push([
'_trackEvent',
"Settings",
2013-06-27 23:10:04 +00:00
'editorFontFamily',
"" + settings.editorFontFamily
2013-06-12 00:00:12 +00:00
]);
2013-06-27 23:10:04 +00:00
_gaq.push([
'_trackEvent',
"Settings",
'editorFontSize',
"" + settings.editorFontSize
]);
2013-06-12 00:00:12 +00:00
// Check if user has removed back links
_gaq.push([
'_trackEvent',
"Settings",
'defaultContent backlink',
"" + (settings.defaultContent.indexOf(MAIN_URL) !== -1)
]);
_gaq.push([
'_trackEvent',
"Settings",
'commitMsg backlink',
"" + (settings.commitMsg.indexOf(MAIN_URL) !== -1)
]);
// Check if user has changed sshProxy
_gaq.push([
'_trackEvent',
"Settings",
'sshProxy unchanged',
"" + (settings.sshProxy == SSH_PROXY_URL)
]);
// Check if extensions have been disabled
_.each(settings.extensionSettings, function(config, extensionId) {
_gaq.push([
'_trackEvent',
"Extensions",
extensionId + " enabled",
"" + (config.enabled === true)
]);
});
// Catch window JavaScript errors
window.onerror = function(message, url, line) {
_gaq.push([
"_trackEvent",
2013-06-12 20:37:43 +00:00
"Error",
2013-06-16 18:29:54 +00:00
message,
url + ":" + line + utils.formatEventList()
2013-06-12 00:00:12 +00:00
]);
};
init();
};
2013-06-10 21:22:32 +00:00
googleAnalytics.onOfflineChanged = function(isOfflineParam) {
isOffline = isOfflineParam;
init();
};
2013-06-10 22:32:34 +00:00
var startTime = 0;
googleAnalytics.onSyncRunning = function(isRunning) {
if(isRunning === true) {
startTime = new Date().getTime();
}
2013-06-10 21:22:32 +00:00
};
2013-06-10 22:32:34 +00:00
googleAnalytics.onPublishRunning = function(isRunning) {
if(isRunning === true) {
startTime = new Date().getTime();
}
2013-06-10 21:22:32 +00:00
};
2013-06-10 22:32:34 +00:00
// Log sync time
2013-06-10 21:22:32 +00:00
googleAnalytics.onSyncSuccess = function() {
2013-06-10 22:32:34 +00:00
var endTime = new Date().getTime();
2013-06-10 21:22:32 +00:00
_gaq.push([
2013-06-10 22:32:34 +00:00
'_trackTiming',
2013-06-10 21:22:32 +00:00
'Sync',
2013-06-10 22:32:34 +00:00
'SyncTime',
endTime - startTime
2013-06-10 21:22:32 +00:00
]);
};
// Log import frequency and provider
googleAnalytics.onSyncImportSuccess = function(fileDescList, provider) {
_gaq.push([
'_trackEvent',
'Sync',
'SyncImport',
]);
_gaq.push([
'_trackEvent',
'Sync',
2013-06-10 22:32:34 +00:00
'SyncImport provider',
2013-06-10 21:22:32 +00:00
provider.providerId
]);
};
// Log export frequency and provider
googleAnalytics.onSyncExportSuccess = function(fileDesc, syncAttributes) {
_gaq.push([
'_trackEvent',
'Sync',
'SyncExport',
]);
_gaq.push([
'_trackEvent',
'Sync',
2013-06-10 22:32:34 +00:00
'SyncExport provider',
2013-06-10 21:22:32 +00:00
syncAttributes.provider.providerId
]);
};
2013-06-10 22:32:34 +00:00
// Log publish time and provider
2013-06-10 21:22:32 +00:00
googleAnalytics.onPublishSuccess = function(fileDesc) {
2013-06-10 22:32:34 +00:00
var endTime = new Date().getTime();
2013-06-10 21:22:32 +00:00
_gaq.push([
2013-06-10 22:32:34 +00:00
'_trackTiming',
2013-06-10 21:22:32 +00:00
'Publish',
2013-06-10 22:32:34 +00:00
'PublishSuccess',
endTime - startTime
2013-06-10 21:22:32 +00:00
]);
_.each(fileDesc.publishLocations, function(publishAttributes) {
_gaq.push([
'_trackEvent',
'Publish',
2013-06-10 22:32:34 +00:00
'PublishSuccess provider',
2013-06-10 21:22:32 +00:00
publishAttributes.provider.providerId
]);
});
};
// Log new publication's provider
googleAnalytics.onNewPublishSuccess = function(fileDesc, publishAttributes) {
_gaq.push([
'_trackEvent',
'Publish',
2013-06-10 22:32:34 +00:00
'NewPublish provider',
2013-06-10 21:22:32 +00:00
publishAttributes.provider.providerId
]);
};
// Log error messages
googleAnalytics.onError = function(error) {
if(_.isString(error) || !error.message) {
return;
}
_gaq.push([
'_trackEvent',
"Error",
"message",
2013-06-16 18:29:54 +00:00
error.message + utils.formatEventList()
]);
};
2013-06-10 21:22:32 +00:00
return googleAnalytics;
});