Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. /**
  2. * Veeva ClickStream tracking class.
  3. *
  4. * Author: Jeremy Daley
  5. * Liscence: MIT
  6. */
  7.  
  8. ClickStream.prototype.virtualInterval = -1;
  9. ClickStream.prototype.calls = []; // Queued calls
  10. ClickStream.prototype.debug = false;
  11.  
  12. function ClickStream(debug){
  13. if (debug !== undefined)
  14. this.debug = debug;
  15. }
  16.  
  17. /**
  18. * This is an event call, with a zero duration. It's used for tracking
  19. * things like clicks.
  20. *
  21. * @param {string} id Track Element Id
  22. * @param {string} type Track Element Type
  23. * @param {string} description Track Element Description
  24. */
  25. ClickStream.prototype.trackEvent = function(id, type, description){
  26. var self = this;
  27. var obj = {
  28. 'Track_Element_Id_vod__c': id,
  29. 'Track_Element_Type_vod__c': type,
  30. 'Track_Element_Description_vod__c': description,
  31. 'Usage_Start_Time_vod__c': new Date(),
  32. 'Usage_Duration_vod__c': 1
  33. };
  34.  
  35. if (self.debug)
  36. console.log('trackEvent:', obj);
  37.  
  38. self.queue('create', 'Call_Clickstream_vod__c', null, obj, self.noop);
  39. };
  40.  
  41. /**
  42. * This is a page tracking call, that should be started at the load of a page. It uses a
  43. * timer to continually update the duration on the page
  44. *
  45. * @param {string} id Track Element Id
  46. * @param {string} type Track Element Type
  47. * @param {string} description Track Element Description
  48. */
  49. ClickStream.prototype.trackPage = function() {
  50. var self = this;
  51. var obj = {
  52. 'Track_Element_Type_vod__c': 'Page View',
  53. 'Usage_Start_Time_vod__c': new Date(),
  54. 'Usage_Duration_vod__c': 1
  55. };
  56.  
  57. if (self.debug)
  58. console.log('trackPage:', obj);
  59.  
  60. self.queue('create', 'Call_Clickstream_vod__c', null, obj, function(data){
  61. var duration = 1;
  62. // Every second, we need to update the Duration field for this record
  63. window.setInterval(function(){
  64. duration++;
  65. obj['Usage_Duration_vod__c'] = duration;
  66.  
  67. self.queue('update', 'Call_Clickstream_vod__c', data.Call_Clickstream_vod__c.ID, obj, self.noop);
  68. }, 1000);
  69. });
  70. };
  71.  
  72. /**
  73. * Track a section within a key message, like for tabs
  74. * @param {string} pageName The name of the virtual section
  75. */
  76. ClickStream.prototype.trackVirtualPage = function(id, description){
  77. // First clear the tracking of a previous virtual pageview
  78. window.clearInterval( this.virtualInterval );
  79.  
  80. var self = this;
  81. var obj = {
  82. 'Track_Element_Id_vod__c': id,
  83. 'Track_Element_Description_vod__c': description,
  84. 'Track_Element_Type_vod__c': 'In-Page View',
  85. 'Usage_Start_Time_vod__c': new Date(),
  86. 'Usage_Duration_vod__c': 1
  87. };
  88.  
  89. if (self.debug)
  90. console.log('trackVirtualPage:', obj);
  91.  
  92. self.queue('create', 'Call_Clickstream_vod__c', null, obj, function(data){
  93. var duration = 1;
  94. // Every second, we need to update the Duration field for this record
  95. self.virtualInterval = window.setInterval(function(){
  96. duration++;
  97. obj['Usage_Duration_vod__c'] = duration;
  98.  
  99. self.queue('update', 'Call_Clickstream_vod__c', data.Call_Clickstream_vod__c.ID, obj, self.noop);
  100. }, 1000);
  101. });
  102. };
  103.  
  104. /**
  105. * Veeva can't take more than one action at a time, so we have to queue up each call
  106. * @param {string} action Something like 'Call_Clickstream_vod__c'
  107. * @param {string} type 'update' or 'create'
  108. * @param {object} obj Tracking fields/values to send to Veeva
  109. * @param {Function} callback
  110. */
  111. ClickStream.prototype.queue = function(action, type, id, obj, callback){
  112. var self = this;
  113. var call = {
  114. action: action,
  115. type: type,
  116. id: id,
  117. obj: obj,
  118. callback: callback
  119. };
  120. this.calls.push( call );
  121.  
  122. // If this is the first item in the queue, go ahead and call it right away
  123. if (this.calls.length == 1)
  124. makeCall( call );
  125.  
  126. function makeCall(call){
  127. console.log(call);
  128. switch( call.action ) {
  129. case 'update':
  130. com.veeva.clm.updateRecord(call.type, call.id, call.obj, queueCallback);
  131. break;
  132. case 'create':
  133. com.veeva.clm.createRecord(call.type, call.obj, queueCallback);
  134. break;
  135. }
  136. // If we're running in debug mode, we wanna make act like the records were successfully created/updated
  137. if (self.debug === true)
  138. queueCallback( {Call_Clickstream_vod__c: {ID: new Date().getTime()}} );
  139. }
  140.  
  141. function queueCallback(data, _action){
  142. // Call the original callback function
  143. self.calls[0].callback( data );
  144. // Remove the current item in the queue
  145. self.calls.shift();
  146.  
  147. // If there's anything left in the queue, let's call it.
  148. if (self.calls.length > 0)
  149. makeCall( self.calls[0] );
  150. }
  151. }
  152.  
  153. /**
  154. * A callback "ground".
  155. */
  156. ClickStream.prototype.noop = function(data){
  157. // Do nothing
  158. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement