Last active
June 21, 2016 00:28
-
-
Save ifnull/a4c0071dc12454fb51e088f6e53b314f to your computer and use it in GitHub Desktop.
Omniture Measurement Library for Node - Experimental port from official PHP package.
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
'use strict'; | |
const OmnitureMeasurement = require('./omniture-measurement'); | |
const account = 'tar***************************'; | |
const trackingServer = 'metrics.*****.com'; | |
// Use the chatId from kik | |
var visitorId = 'b3be3bc15dbe59931***********************0afab'; | |
var s = new OmnitureMeasurement(); | |
s.trackingServer = trackingServer; | |
s.account = account; | |
s.ssl = false; | |
s.logging = true; | |
s.visitorID = visitorId; | |
// Example | |
s.pageName = 'Server-To-Server Test'; | |
s.channel = null; | |
s.prop1 = null; | |
s.track(); |
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
'use strict'; | |
// OmnitureMeasurement constructor. | |
var OmnitureMeasurement = function() { | |
// setup required var list | |
this.requiredVarList = [ | |
"dynamicVariablePrefix", | |
"visitorID", | |
"vmk", | |
"visitorMigrationKey", | |
"visitorMigrationServer", | |
"visitorMigrationServerSecure", | |
"charSet", | |
"visitorNamespace", | |
"cookieDomainPeriods", | |
"cookieLifetime", | |
"pageName", | |
"pageURL", | |
"referrer", | |
"currencyCode", | |
"timestamp", | |
"pe", | |
"pev1", | |
"pev2" | |
]; | |
// setup account var list | |
this.accountVarList = this.requiredVarList.concat([ | |
"purchaseID", | |
"variableProvider", | |
"channel", | |
"server", | |
"pageType", | |
"transactionID", | |
"campaign", | |
"state", | |
"zip", | |
"events", | |
"products" | |
]); | |
// setup account config list | |
this.accountConfigList = [ | |
"linkURL", | |
"linkName", | |
"linkType", | |
"linkTrackVars", | |
"linkTrackEvents", | |
"dc", | |
"trackingServer", | |
"trackingServerSecure", | |
"userAgent" | |
]; | |
// public variables (properties) | |
this.trackingServer = null; | |
this.ssl = true; | |
this.version = "PHP-1.1S"; | |
this.logging = false; | |
this.account = null; | |
this.linkURL = null; | |
this.linkName = null; | |
this.linkType = null; | |
this.linkTrackVars = null; | |
this.linkTrackEvents = null; | |
this.dc = null; | |
this.userAgent = null; | |
this.dynamicVariablePrefix = null; | |
this.visitorID = null; | |
this.vmk = null; | |
this.visitorMigrationKey = null; | |
this.visitorMigrationServer = null; | |
this.visitorMigrationServerSecure = null; | |
this.charSet = null; | |
this.visitorNamespace = null; | |
this.cookieDomainPeriods = null; | |
this.cookieLifetime = null; | |
this.pageName = null; | |
this.pageURL = null; | |
this.referrer = null; | |
this.currencyCode = null; | |
this.purchaseID = null; | |
this.variableProvider = null; | |
this.channel = null; | |
this.server = null; | |
this.pageType = null; | |
this.transactionID = null; | |
this.campaign = null; | |
this.state = null; | |
this.zip = null; | |
this.events = null; | |
this.products = null; | |
this.hier1 = null; | |
this.hier2 = null; | |
this.hier3 = null; | |
this.hier4 = null; | |
this.hier5 = null; | |
this.prop1 = null; | |
this.prop2 = null; | |
this.prop3 = null; | |
this.prop4 = null; | |
this.prop5 = null; | |
this.prop6 = null; | |
this.prop7 = null; | |
this.prop8 = null; | |
this.prop9 = null; | |
this.prop10 = null; | |
this.prop11 = null; | |
this.prop12 = null; | |
this.prop13 = null; | |
this.prop14 = null; | |
this.prop15 = null; | |
this.prop16 = null; | |
this.prop17 = null; | |
this.prop18 = null; | |
this.prop19 = null; | |
this.prop20 = null; | |
this.prop21 = null; | |
this.prop22 = null; | |
this.prop23 = null; | |
this.prop24 = null; | |
this.prop25 = null; | |
this.prop26 = null; | |
this.prop27 = null; | |
this.prop28 = null; | |
this.prop29 = null; | |
this.prop30 = null; | |
this.prop31 = null; | |
this.prop32 = null; | |
this.prop33 = null; | |
this.prop34 = null; | |
this.prop35 = null; | |
this.prop36 = null; | |
this.prop37 = null; | |
this.prop38 = null; | |
this.prop39 = null; | |
this.prop40 = null; | |
this.prop41 = null; | |
this.prop42 = null; | |
this.prop43 = null; | |
this.prop44 = null; | |
this.prop45 = null; | |
this.prop46 = null; | |
this.prop47 = null; | |
this.prop48 = null; | |
this.prop49 = null; | |
this.prop50 = null; | |
this.eVar1 = null; | |
this.eVar2 = null; | |
this.eVar3 = null; | |
this.eVar4 = null; | |
this.eVar5 = null; | |
this.eVar6 = null; | |
this.eVar7 = null; | |
this.eVar8 = null; | |
this.eVar9 = null; | |
this.eVar10 = null; | |
this.eVar11 = null; | |
this.eVar12 = null; | |
this.eVar13 = null; | |
this.eVar14 = null; | |
this.eVar15 = null; | |
this.eVar16 = null; | |
this.eVar17 = null; | |
this.eVar18 = null; | |
this.eVar19 = null; | |
this.eVar20 = null; | |
this.eVar21 = null; | |
this.eVar22 = null; | |
this.eVar23 = null; | |
this.eVar24 = null; | |
this.eVar25 = null; | |
this.eVar26 = null; | |
this.eVar27 = null; | |
this.eVar28 = null; | |
this.eVar29 = null; | |
this.eVar30 = null; | |
this.eVar31 = null; | |
this.eVar32 = null; | |
this.eVar33 = null; | |
this.eVar34 = null; | |
this.eVar35 = null; | |
this.eVar36 = null; | |
this.eVar37 = null; | |
this.eVar38 = null; | |
this.eVar39 = null; | |
this.eVar40 = null; | |
this.eVar41 = null; | |
this.eVar42 = null; | |
this.eVar43 = null; | |
this.eVar44 = null; | |
this.eVar45 = null; | |
this.eVar46 = null; | |
this.eVar47 = null; | |
this.eVar48 = null; | |
this.eVar49 = null; | |
this.eVar50 = null; | |
this.list1 = null; | |
this.list2 = null; | |
this.list3 = null; | |
this.timestamp = null; | |
this.linkLeaveQueryString = null; | |
this.mobile = null; | |
this.debugTracking = null; | |
this.debugFilename = null; | |
this.sendFromServer = true; | |
this.imageDimensions = null; | |
}; | |
// Test | |
OmnitureMeasurement.prototype.test = function(text) { | |
return this.trackingServer; | |
}; | |
// Logging | |
OmnitureMeasurement.prototype.log = function(data) { | |
if (this.logging) { | |
console.log(data); | |
} | |
}; | |
// Random number generator | |
OmnitureMeasurement.prototype.random = function(low, high) { | |
return Math.floor(Math.random() * (high - low) + low); | |
}; | |
// URL encode | |
OmnitureMeasurement.prototype.escape = function(unescapedString) { | |
return encodeURIComponent(unescapedString); | |
}; | |
// URL encode | |
OmnitureMeasurement.prototype.getFormattedTimestamp = function() { | |
var date = new Date(); | |
var dateParts = [ | |
date.getDate(), | |
'/', | |
date.getMonth(), | |
'/', | |
date.getFullYear(), | |
' ', | |
date.getHours(), | |
':', | |
(date.getMinutes()<10?'0':'') + date.getMinutes(), | |
':', | |
(date.getSeconds()<10?'0':'') + date.getSeconds(), | |
' ', | |
date.getDay(), | |
' ', | |
date.getTimezoneOffset() | |
]; | |
return dateParts.join(''); | |
}; | |
// Make Request | |
OmnitureMeasurement.prototype.makeRequest = function(cacheBusting, queryString) { | |
var s = this; | |
// build request | |
var trackingServer = s.trackingServer; | |
var requestProtocol = (s.ssl ? 'https:' : 'http:'); | |
var returnType = 0; | |
var version = s.version; | |
var requestPathParts = ['/b/ss/', s.account, '/', returnType, '/', version, '/', | |
cacheBusting, '?AQB=1&ndh=1&', queryString, '&AQE=1']; | |
var requestOptions = { | |
'protocol': requestProtocol, | |
'host': trackingServer, | |
'path': requestPathParts.join('') | |
}; | |
// send request | |
s.log(requestOptions); | |
s.sendRequest(requestOptions); | |
}; | |
// Send Request | |
OmnitureMeasurement.prototype.sendRequest = function(requestOptions) { | |
var s = this; | |
var h; | |
s.log('[sendRequest]'); | |
if (requestOptions.protocol == 'https:') { | |
h = require('https'); | |
} else { | |
h = require('http'); | |
} | |
var callback = function(response) { | |
var str = ''; | |
//another chunk of data has been recieved, so append it to `str` | |
response.on('data', function (chunk) { | |
str += chunk; | |
}); | |
//the whole response has been recieved, so we just print it out here | |
response.on('end', function () { | |
s.log(str); | |
s.log(response.rawHeaders); | |
s.log(response.req._headers); | |
s.log(response.req.path); | |
}); | |
}; | |
h.request(requestOptions, callback).end(); | |
}; | |
// Track | |
OmnitureMeasurement.prototype.track = function(variableOverrides) { | |
var s = this; | |
var variableOverridesBackup = []; | |
// build cacheBusting | |
var cacheBusting = 's' + this.random(1, 1000000000); | |
// build timestamp (start of query string) | |
var queryString = 't=' + s.escape(s.getFormattedTimestamp()); | |
// apply variable overrides (if applicable) | |
if (typeof variableOverrides !== 'undefined') { | |
variableOverridesBackup = s.variableOverridesBuild(variableOverridesBackup); | |
variableOverrides = s.variableOverridesApply(variableOverrides); | |
} | |
// build query string and make request | |
if (s.isSetString(s.account)) { | |
queryString = queryString + s.getQueryString(); | |
s.log(cacheBusting); | |
s.log(queryString); | |
s.makeRequest(cacheBusting, queryString); | |
} | |
// restore variables (if applicable) | |
if (typeof variableOverrides !== 'undefined') { | |
s.variableOverridesApply(variableOverridesBackup); | |
} | |
// reset one-timers | |
s.referrer = ''; | |
s.pe = ''; | |
s.pev1 = ''; | |
s.pev2 = ''; | |
s.linkURL = ''; | |
s.linkName = ''; | |
s.linkType = ''; | |
}; | |
// variableOverridesBuild | |
OmnitureMeasurement.prototype.variableOverridesBuild = function(variableOverrides) { | |
var s = this; | |
s.accountVarList.forEach(function(varKey, i, a){ | |
var varValue = s.getAccountVar(varKey); | |
var overrideValue = variableOverrides[varKey]; | |
// overwrite the override value if it's not set | |
if (!s.isSetString(overrideValue)) { | |
if (s.isSetString(varValue)) { | |
variableOverrides[varKey] = varValue; | |
} else { | |
variableOverrides["!"+varKey] = '1'; // "1" simply acts as a mark that the variable was not set | |
} | |
} | |
}); | |
s.accountConfigList.forEach(function(varKey, i, a){ | |
var varValue = s.getAccountVar(varKey); | |
var overrideValue = variableOverrides[varKey]; | |
// overwrite the override value if it's not set | |
if (!s.isSetString(overrideValue)) { | |
if (s.isSetString(varValue)) { | |
variableOverrides[varKey] = varValue; | |
} else { | |
variableOverrides["!"+varKey] = '1'; // "1" simply acts as a mark that the variable was not set | |
} | |
} | |
}); | |
return variableOverrides; | |
}; | |
// variableOverridesApply | |
OmnitureMeasurement.prototype.variableOverridesApply = function(variableOverrides) { | |
var s = this; | |
s.accountVarList.forEach(function(varKey, i, a){ | |
var overrideValue = variableOverrides[varKey]; | |
if (s.isSetString(overrideValue) || s.isSetString(variableOverrides["!"+varKey])) { | |
s.setAccountVar(varKey, overrideValue); | |
} | |
}); | |
s.accountConfigList.forEach(function(varKey, i, a){ | |
var overrideValue = variableOverrides[varKey]; | |
if (s.isSetString(overrideValue) || s.isSetString(variableOverrides["!"+varKey])) { | |
s.setAccountVar(varKey, overrideValue); | |
} | |
}); | |
return variableOverrides; | |
}; | |
// isNumber | |
OmnitureMeasurement.prototype.isNumber = function(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
}; | |
// Set Account Var | |
OmnitureMeasurement.prototype.setAccountVar = function(key, value) { | |
var s = this; | |
s[key] = value; | |
}; | |
// Get Account Var | |
OmnitureMeasurement.prototype.getAccountVar = function(key) { | |
var s = this; | |
return s[key]; | |
}; | |
// Is string set | |
OmnitureMeasurement.prototype.isSetString = function(v) { | |
var s = this; | |
return (v !== '' && v !== null && typeof v !== 'undefined'); | |
}; | |
// getQueryString | |
OmnitureMeasurement.prototype.getQueryString = function() { | |
var s = this, varPrefix, varSuffix; | |
var queryString = '&cl=none'; // cookie lifetime => no cookies | |
var ignoreCookieLifetime = true; // ignore any user setting for cookie lifetime | |
s.accountVarList.forEach(function(varKey, i, a){ | |
var varValue = s.getAccountVar(varKey); | |
if (varKey.length > 4) { | |
varPrefix = varKey.substr(0, 4); | |
varSuffix = varKey.substr(4); | |
} else { | |
varPrefix = ''; | |
varSuffix = ''; | |
} | |
if (s.isSetString(varValue)) { | |
// key transformation (and applying event filters, etc.) | |
if (varKey == "dynamicVariablePrefix") { | |
varKey = "D"; | |
} else if (varKey == "visitorID") { | |
varKey = "vid"; | |
} else if (varKey == "pageURL") { | |
varKey = "g"; | |
varValue = varValue.substr(0, 255); | |
} else if (varKey == "referrer") { | |
varKey = "r"; | |
varValue = varValue.substr(0, 255); | |
} else if (varKey == "vmk" || varKey == "visitorMigrationKey") { | |
varKey = "vmt"; | |
} else if (varKey == "visitorMigrationServer") { | |
varKey = "vmf"; | |
if (s.ssl && s.visitorMigrationServerSecure) { | |
varValue = ""; | |
} | |
} else if (varKey == "visitorMigrationServerSecure") { | |
varKey = "vmf"; | |
if (!s.ssl && s.visitorMigrationServer) { | |
varValue = ""; | |
} | |
} else if (varKey == "timestamp") { | |
varKey = "ts"; | |
} else if (varKey == "pageName") { | |
varKey = "gn"; | |
} else if (varKey == "pageType") { | |
varKey = "gt"; | |
} else if (varKey == "products") { | |
varKey = "pl"; | |
} else if (varKey == "purchaseID") { | |
varKey = "pi"; | |
} else if (varKey == "server") { | |
varKey = "sv"; | |
} else if (varKey == "charSet") { | |
varKey = "ce"; | |
} else if (varKey == "visitorNamespace") { | |
varKey = "ns"; | |
} else if (varKey == "cookieDomainPeriods") { | |
varKey = "cdp"; | |
} else if (varKey == "cookieLifetime") { | |
if (ignoreCookieLifetime) { // ignore cookie lifetime when overridden above | |
return; | |
} | |
varKey = "cl"; | |
} else if (varKey == "currencyCode") { | |
varKey = "cc"; | |
} else if (varKey == "channel") { | |
varKey = "ch"; | |
} else if (varKey == "transactionID") { | |
varKey = "xact"; | |
} else if (varKey == "campaign") { | |
varKey = "v0"; | |
} else if (varKey == "events") { | |
varKey = "ev"; | |
} else if (s.isNumber(varSuffix)) { | |
if (varPrefix == "prop") { | |
varKey = "c" + varSuffix; | |
} else if (varPrefix == "eVar") { | |
varKey = "v" + varSuffix; | |
} else if (varPrefix == "list") { | |
varKey = "l" + varSuffix; | |
} else if (varPrefix == "hier") { | |
varKey = "h" + varSuffix; | |
varValue = varValue.substr(0, 255); | |
} | |
} | |
// do not escape pev1/pev2 values because they were already escaped beforehand (this is to only escape if we have to) | |
if (s.isSetString(varValue)) { | |
queryString = queryString + '&' + s.escape(varKey) + '=' + (varKey.substr(0, 3) == 'pev' ? varValue : s.escape(varValue)); | |
} | |
} | |
}); | |
return queryString; | |
}; | |
// Export the OmnitureMeasurement constructor from this module. | |
module.exports = OmnitureMeasurement; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment