Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

Commit

Permalink
Attempt to fix google analytics priming problem. Use manual __utm.gif
Browse files Browse the repository at this point in the history
  • Loading branch information
unconed committed Jun 13, 2011
1 parent 96152ec commit 1b92ca4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
4 changes: 2 additions & 2 deletions HTML/client/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tc.shell.prototype = {

switch (method) {

case 'view.clear':
case 'shell.clear':
this.commandView && this.commandView.clear();
break;

Expand Down Expand Up @@ -121,7 +121,7 @@ tc.shell.prototype = {
[
'v' + window.preferences.get('version'),
this.anonymize(tokens),
Math.floor(Math.random() * 1000)
Math.floor(Math.random() * 100000)
].join('--@--'));
}
else {
Expand Down
114 changes: 112 additions & 2 deletions HTML/usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@
Note: anonymous usage logging. Can be turned off in the preferences.
Is served from https://usage.termkit.org/ to trigger SSL on the Google Analytics data, ensuring privacy.
Uses Google Analytics over SSL.
Ga.js doesn't work without being primed in a real browser first, so we make manual utm.gif requests.
-->

<!--
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-288349-5']);
_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);
})();
</script>
-->

<script type="text/javascript">

// Track each session as a pageview.
Expand All @@ -22,9 +43,93 @@

(function() {
// Inject Analytics (SSL).
/*
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
*/

function getRandom() {
return Math.floor(Math.random() * 1000000000);
}

var cookie = '',
uniqueId = getRandom(),
accountId = 'UA-288349-5';

makeCookie();

/**
* Generate ga.js 'cookie'.
*/
function makeCookie() {
var id = getRandom();
random = getRandom() & 0xFFFFFFFF,
today = Math.floor(+new Date() * .001);
cookie = '__utma=1.'+ id + '.' + random + '.' + today + '.' + today + '.15;+__utmz=1.' + today + '.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);';
}

/**
* Shared GA parameters.
*/
function gaParameters() {
return {
utmwv: '4.3',
utmn: (getRandom() * 17) & 0xFFFFFFFF,
utmhn: 'usage.termkit.org',
utmcs: 'utf-8',
utmul: navigator.language,
utmhid: uniqueId,
utmac: accountId,
utmcc: cookie,
};
}

/**
* Trigger GA page view.
*/
function gaPageView(path) {
var parameters = gaParameters();

parameters.utmdt = 'TermKit';
parameters.utmp = path;
parameters.utmsr = window.screen.width + 'x' + window.screen.height;
parameters.utmsc = (window.screen.colorDepth >= 24 ? 32 : window.screen.colorDepth) + '-bit';

gaRequest(parameters);
);

/**
* Trigger GA event.
*/
function gaEvent(category, action) {

var parameters = gaParameters();

parameters.utmt = 'event';
parameters.utme = '5('+ encodeURIComponent(category) + '*' + encodeURIComponent(action) + ')';

gaRequest(parameters);
);

/**
* ga.js doesn't work within WebView unless primed by browser first.
*
* Hack together our own __utm.gif requests.
*/
function gaRequest(parameters) {

// Prepare parameters.
var query = [];;
for (var i in parameters) {
query.push(encodeURIComponent(i) +'='+ encodeURIComponent(parameters[i]));
}

// Load __utm.gif.
var url = 'https://ssl.google-analytics.com/__utm.gif?' + query.join('&'),
img = new Image();
img.src = url;
}

/**
* Log an event.
Expand All @@ -37,13 +142,18 @@
// Log versioned pageview.
if (first) {
first = false;
/*
_gaq.push(['_setAccount', 'UA-288349-5']);
_gaq.push(['_setDomainName', 'usage.termkit.org']);
_gaq.push(['_trackPageview', '/' + version]);
*/
gaPageView('/' + version);
}

// Include random delay of up to 10 seconds for increased privacy.
setTimeout(function () {
_gaq.push(['_trackEvent', 'shell', tokens]);
//_gaq.push(['_trackEvent', 'shell', tokens]);
gaEvent('shell', tokens);
}, Math.random() * 10000);
}

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var view = require('view/view');

exports.main = function (tokens, pipes, exit, environment) {

pipes.viewOut('view.clear');
pipes.viewOut('shell.clear');

exit(true);
};

0 comments on commit 1b92ca4

Please sign in to comment.