-
-
Save alexanderdean/4619940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Alias. Worth keeping in because it's so verbose. | |
var encodeWrapper = window.encodeURIComponent; | |
// IMPORTANT!! | |
// ------------------------------------------ | |
// This is how simple it is to build the final URL: | |
image.src = configCollectorUrl + '?' + request; | |
// See below for how each piece (configCollectorUrl and request) are built... | |
/** | |
* Builds a collector URL from a CloudFront distribution. | |
* We don't bother to support custom CNAMEs because Amazon CloudFront doesn't support that for SSL. | |
* | |
* @param string account The account ID to build the tracker URL from | |
* | |
* @return string The URL on which the collector is hosted | |
*/ | |
function collectorUrlFromCfDist(distSubdomain) { | |
return asCollectorUrl(distSubdomain + '.cloudfront.net'); | |
} | |
/** | |
* Adds the protocol in front of our collector URL, and i to the end | |
* | |
* @param string rawUrl The collector URL without protocol | |
* | |
* @return string collectorUrl The tracker URL with protocol | |
*/ | |
function asCollectorUrl(rawUrl) { | |
return ('https:' == document.location.protocol ? 'https' : 'http') + '://' + rawUrl + '/i'; | |
} | |
// This is a kind of "expanded out" function which includes logging page | |
// view and the getRequest code. So you can see it all in one place. | |
function logPageView(customTitle) { | |
// Fixup page title. We'll pass this to logPagePing too. | |
var pageTitle = SnowPlow.fixupTitle(customTitle || configTitle); | |
// Log page view | |
var sb = requestStringBuilder(); | |
sb.add('e', 'pv'); // 'pv' for Page View | |
sb.add('page', pageTitle); | |
sb.addRaw('tid', String(Math.random()).slice(2, 8)); | |
sb.addRaw('vp', detectViewport()); | |
sb.addRaw('ds', detectDocumentSize()); | |
// Encode all these | |
sb.add('p', configPlatform); | |
sb.add('tv', SnowPlow.version); | |
sb.add('fp', fingerprint); | |
sb.add('aid', configTrackerSiteId); | |
sb.add('lang', browserLanguage); | |
sb.add('cs', documentCharset); | |
sb.add('tz', timezone); | |
// Adds with custom conditions | |
if (configAttachUserId) sb.addRaw('uid', uuid); | |
if (configReferrerUrl.length) sb.add('refr', purify(configReferrerUrl)); | |
// Browser features. Cookies, color depth and resolution don't get prepended with f_ (because they're not optional features) | |
for (i in browserFeatures) { | |
if (Object.prototype.hasOwnProperty.call(browserFeatures, i)) { | |
featurePrefix = (i === 'res' || i === 'cd' || i === 'cookie') ? '' : 'f_'; | |
sb.addRaw(featurePrefix + i, browserFeatures[i]); | |
} | |
} | |
// Add the page URL last as it may take us over the IE limit (and we don't always need it) | |
sb.add('url', purify(SnowPlow.windowAlias.location)); | |
var request = sb.build(); | |
return request; | |
} | |
/** | |
* A helper to build a SnowPlow request string from an | |
* an optional initial value plus a set of individual | |
* name-value pairs, provided using the add method. | |
* | |
* @param string initialValue The initial querystring, ready to have additional key-value pairs added | |
* | |
* @return object The request string builder, with add, addRaw and build methods | |
*/ | |
function requestStringBuilder(initialValue) { | |
var str = initialValue || ''; | |
var addNvPair = function(key, value, encode) { | |
if (value !== undefined && value !== '') { | |
str += '&' + key + '=' + (encode ? SnowPlow.encodeWrapper(value) : value); | |
} | |
}; | |
return { | |
add: function(key, value) { | |
addNvPair(key, value, true); | |
}, | |
addRaw: function(key, value) { | |
addNvPair(key, value, false); | |
}, | |
build: function() { | |
return str; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment