API Docs for: 0.0.6
Show:

File: lib/omnitureHelper.js

  1. // omnitureHelper.js
  2. // version : 0.0.1
  3. // author : Patrick Gidich
  4. // license : MIT
  5. // https://github.com/simnova/OmnitureHelper
  6.  
  7. /*jslint indent:2, nomen: true, browser:true, plusplus:true, todo:true */
  8. (function ($, _) {
  9. "use strict";
  10.  
  11. var s, //alias to local variable name
  12. _events,
  13. _commerceVariables, //eVar
  14. _insightVariables, //sProp
  15. reservedEventNames = [ //TODO:check for the usage of these
  16. "prodView", // productViews
  17. "scOpen", // Open / Initialize a new shopping cart
  18. "scAdd", // Add item(s) to the shopping cart
  19. "scRemove", // Remove item(s) from the shopping cart
  20. "scView", // View shopping cart
  21. "scCheckout", // Beginning of the checkout process
  22. "purchase" // Completion of a purchase (order)
  23. ],
  24. reservedSpropNames = [ //TODO:check for the usage of these
  25. "campaign",
  26. "state",
  27. "zip",
  28. "events",
  29. ""
  30. ];
  31.  
  32. /**
  33. * OmnitureHelper - a microlibrary for omniture tagging.
  34. *
  35. * @class OmnitureHelper
  36. * @constructor
  37. * @param events {object} (id,name) array of all events that will be used.
  38. * @param commerceVariables {object} (id,name) array of all commerce variables that will be used.
  39. * @param insightVariables {object} (id,name) array of all insight variables that will be used.
  40. */
  41. function OmnitureHelper(events, commerceVariables, insightVariables) {
  42. _events = events;
  43. _commerceVariables = commerceVariables; //eVar
  44. _insightVariables = insightVariables; //sProp
  45. window.s = window.s || {};
  46. s = window.s;
  47. _ = window._ || {};
  48. $ = window.$ || {};
  49. }
  50.  
  51. // Private Methods
  52. function findCommerceVariableId(commerceVariableName) {
  53. var id;
  54. try {
  55. id = _.find(_commerceVariables, function (variable) { return variable.name === commerceVariableName; }).id;
  56. } catch (e) {
  57. throw {
  58. name: "Invalid Argument Exception",
  59. message: "Parameter: 'commerceVariableName' with value :'" + commerceVariableName + "' not found in commerce variable array"
  60. };
  61. }
  62. return id;
  63. }
  64.  
  65. function findInsightVariableId(insightVariableName) {
  66. var id;
  67. try {
  68. id = _.find(_insightVariables, function (variable) { return variable.name === insightVariableName; }).id;
  69. } catch (e) {
  70. throw {
  71. name: "Invalid Argument Exception",
  72. message: "Parameter: 'insightVariableName' with value :'" + insightVariableName + "' not found in insight variable array"
  73. };
  74. }
  75. return id;
  76. }
  77.  
  78. function findEventId(eventName) {
  79. var id;
  80. try {
  81. id = _.find(_events, function (eventElement) { return eventElement.name === eventName; }).id;
  82. } catch (e) {
  83. throw {
  84. name: "Invalid Argument Exception",
  85. message: "Parameter: 'eventName' with value :'" + eventName + "' not found in event array"
  86. };
  87. }
  88. return id;
  89. }
  90.  
  91. // Public Methods
  92. OmnitureHelper.prototype = {
  93.  
  94. /**
  95. * Clears Variables, Page Name and Events. Useful to be used before setting values in a method
  96. *
  97. * @method clearVariablesAndEvents
  98. */
  99. clearVariablesAndEvents: function () {
  100. this.setPageName("");
  101. this.clearCommerceVariables();
  102. this.clearInsightVariables();
  103. this.clearEvents();
  104. },
  105.  
  106. /**
  107. * Clears Every Commerce Variable.
  108. *
  109. * @method clearCommerceVariables
  110. */
  111. clearCommerceVariables: function () {
  112. var value,
  113. i;
  114. for (i = 1; i < 100; i++) {
  115. value = s["eVar" + i];
  116. if (value !== undefined) {
  117. s["eVar" + i] = "";
  118. }
  119. }
  120. },
  121.  
  122. /**
  123. * Lists Every Commerce Variable with a Value.
  124. *
  125. * @method commerceVariablesWithValues
  126. */
  127. commerceVariablesWithValues: function () {
  128. var commerceVariableList = [],
  129. i,
  130. value;
  131. for (i = 1; i < 100; i++) {
  132. value = s["eVar" + i];
  133. // using jquery trim since older browsers don't have trim built in (ie8 etc)
  134. if (value !== undefined && $.trim(value) !== "") {
  135. commerceVariableList.push("eVar" + i);
  136. }
  137. }
  138. return commerceVariableList;
  139. },
  140.  
  141. /**
  142. * Clears Every Insight Variable
  143. *
  144. * @method clearInsightVariables
  145. */
  146. clearInsightVariables: function () {
  147. var value,
  148. i;
  149. for (i = 1; i < 100; i++) {
  150. value = s["prop" + i];
  151. if (value !== undefined) {
  152. s["prop" + i] = "";
  153. }
  154. }
  155. },
  156.  
  157. /**
  158. * Lists Every Insight Variable with a Value.
  159. *
  160. * @method insightVariablesWithValues
  161. */
  162. insightVariablesWithValues: function () {
  163. var commerceVariableList = [],
  164. i,
  165. value;
  166. for (i = 1; i < 100; i++) {
  167. value = s["prop" + i];
  168. // using jquery trim since older browsers don't have trim built in (ie8 etc)
  169. if (value !== undefined && $.trim(value) !== "") {
  170. commerceVariableList.push("prop" + i);
  171. }
  172. }
  173. return commerceVariableList;
  174. },
  175.  
  176. /**
  177. * Set Page Name
  178. *
  179. * @method setPageName
  180. * @param pageName {string} The Name of the Page.
  181. */
  182. setPageName: function (pageName) {
  183. s.pageName = pageName; //max 100 bytes
  184. },
  185.  
  186. /**
  187. * Set Page Url
  188. *
  189. * @method setPageUrl
  190. * @param url {string} The Url To Set.
  191. */
  192. setPageUrl: function (url) {
  193. s.pageURL = url;
  194. },
  195.  
  196. /**
  197. * Set Channel
  198. *
  199. * @method setChannel
  200. * @param channelName {string} The Channel Name.
  201. */
  202. setChannel: function (channelName) {
  203. s.channel = channelName;
  204. },
  205.  
  206. /**
  207. * Used only to designate a 404 Page Not Found Error Page.
  208. *
  209. * @method setErrorPage
  210. */
  211. setErrorPage: function () {
  212. s.pageType = "errorPage";
  213. },
  214.  
  215. /**
  216. * Fire Event (s.events) adds another event to the list of events.
  217. *
  218. * @method fireEvent
  219. * @param eventName {string} The key of the event to be added
  220. */
  221. fireEvent: function (eventName) {
  222. //s.events = _;
  223. //return;
  224. var eventId = findEventId(eventName);
  225. if (s.events) { s.events += ",event" + eventId; } else { s.events = "event" + eventId; }
  226. },
  227.  
  228. /**
  229. * Clear Events (s.events) removes all events.
  230. *
  231. * @method fireEvent
  232. */
  233. clearEvents: function () {
  234. s.events = "";
  235. },
  236.  
  237. /**
  238. * Increment Commerce Variable (s.eVar)
  239. *
  240. * @method incrementCommerceCounterValue
  241. * @param variableName {string} The key associated with the eVar number that is to be incremented.
  242. */
  243. incrementCommerceCounterValue: function (eVarName) {
  244. var eVarId = findCommerceVariableId(eVarName);
  245. s["eVar" + eVarId] = isNaN(s["eVar" + eVarId]) ? 1 : s["eVar" + eVarId] + 1;
  246. },
  247.  
  248. /**
  249. * Set Commerce Variable (s.eVar)
  250. *
  251. * @method setCommerceVariable
  252. * @param variableName {string} The key associated with the eVar number that is to be set.
  253. * @param value {string} The value that is to be recorded.
  254. */
  255. setCommerceVariable: function (variableName, value) {
  256. var variableId = findCommerceVariableId(variableName);
  257. s["eVar" + variableId] = value;
  258. },
  259.  
  260. /**
  261. * Set Insight Variable (s.prop)
  262. *
  263. * @method setInsightVariable
  264. * @param variableName {string} The key associated with the prop number that is to be set.
  265. * @param value {string} The value that is to be recorded.
  266. */
  267. setInsightVariable: function (variableName, value) {
  268. var variableId = findInsightVariableId(variableName);
  269. s["prop" + variableId] = value;
  270. },
  271.  
  272. /**
  273. * used to track evars / props with associated page info.
  274. *
  275. * @method trackVirtualPageView
  276. */
  277. trackVirtualPageView: function () {
  278. var fn = s.t;
  279. fn.call(window.s);
  280. },
  281.  
  282. /**
  283. * used to track evars / props WITHOUT associated page info.
  284. *
  285. * @method saveValues
  286. * @param thisvar {object} this
  287. */
  288. saveValues: function (thisvar) {
  289. //make sure we handle null values
  290. s.linkTrackEvents = s.linkTrackEvents || "";
  291. s.linkTrackVars = s.linkTrackVars || "";
  292. s.events = s.events || "";
  293.  
  294. var fn = s.tl,
  295. linkType = 'o', // Custom Link
  296. linkName = 'action',
  297. variableOverrides = null,
  298. doneAction = null, // we're not using
  299. //note all events and variables that have values
  300. activeCommerceVariables = this.commerceVariablesWithValues(), // evars
  301. activeInsightVariables = this.insightVariablesWithValues(), // props
  302. activeEvents = s.events.split(","), // events
  303. linkTrackEventArray = s.linkTrackEvents.split(","),
  304. linkTrackVarArray = s.linkTrackVars.split(",");
  305.  
  306. // force omniture to save values we have set.
  307. // See #2 in link below:
  308. // http://blogs.adobe.com/digitalmarketing/analytics/top-five-javascript-implementation-gotchas/
  309. s.linkTrackEvents = _.chain(_.union(linkTrackEventArray, activeEvents)).uniq().without("").value().join();
  310. s.linkTrackVars = _.chain(_.union(linkTrackVarArray, activeCommerceVariables, activeInsightVariables, ["events"])).without("").value().join();
  311.  
  312. //fn.call(window.s, thisvar, linkType, linkName, variableOverrides);
  313. fn.call(window.s, thisvar, linkType, linkName);
  314. },
  315.  
  316. /**
  317. * used to track exit link.
  318. *
  319. * @method saveValues
  320. * @param thisvar {object} this
  321. * @param linkName {string} the url or some other decription of where the user is going..
  322. */
  323. saveExitLink: function (thisvar, linkName) {
  324. var fn = s.tl,
  325. linkType = 'e', // Exit Link
  326. variableOverrides = null,
  327. doneAction = null; // we're not using
  328. fn.call(window.s, thisvar, linkType, linkName, variableOverrides);
  329. }
  330.  
  331. };
  332.  
  333.  
  334. // AMD / RequireJS
  335. if (window !== undefined
  336. && window.define !== undefined
  337. && window.define.amd !== undefined) {
  338. window.define(['jquery', 'lodash', 'omniture'], function ($, _) {
  339. return OmnitureHelper;
  340. });
  341. } else { // included directly via <script> tag
  342. window.OmnitureHelper = OmnitureHelper;
  343. }
  344.  
  345. }());