Advertisement
Guest User

Adobe Analytics s.getTimeBetweenEvents

a guest
Feb 6th, 2018
564
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.21 KB | None
  1. /**
  2.  * Plugin: lPS (leftPadString)
  3.  * Acronym Media 2013.12.23
  4.  * @param  str  v value to pad
  5.  * @param  int  w total width (chars) of string
  6.  * @param  str  z value to use as pad (default 0)
  7.  * @return str  left padded value
  8.  **/
  9. s.lPS = new Function("v", "w", "z", "" +
  10.     "z=String(z||'0');v=v+'';return v.length>=w?v:new Array(w-v.length+1" +
  11.     ").join(z)+v;");
  12. // end leftPadString
  13.  
  14. /*
  15.   This function takes two js timestamps and calculates the difference
  16.   between the two and returns the difference in a d:h:m format.
  17.  
  18.   By default, it does NOT round hours or minutes.
  19.      
  20.   Optional 3rd arg will round UP or DOWN after the first hour and first day.
  21.   Ex: 0:1:12 will be rounded up to 0:2:0 or down to 0:1:0
  22.       1:5:6 will be rounded up to 2:0:0 or down to 1:0:0
  23.  
  24.   Optional 4th arg will pad the d:h:m value with leading 0's.
  25.   Ex: 0:0:1 can be turned into 00:00:01
  26. */
  27.  
  28. /**
  29.  * Plugin: getTimeDifference
  30.  * Acronym Media 2013.12.23
  31.  * Dependencies: s.lPS (leftPadString)
  32.  * @param  str  b start timestamp
  33.  * @param  str  e end timestamp
  34.  * @param  bool r round hours/mins up or down. default down.
  35.  * @param  int  p number of 0's to pad d:h:m with
  36.  * @return str [days]:[hours]:[minutes] diff between b and e
  37.  **/
  38. s.getTimeDifference = new Function("b", "e", "r", "p", "" +
  39.     "var s=this;var p=Number(p||0);var r=r||'';if(r!='u'||r!=='d')r==fal" +
  40.     "se;var t=new Date();t.setTime(e-b);td=t.getTime();var d=Math.floor(" +
  41.     "td/86400000);td-=d*86400000;var h=Math.floor(td/3600000);td-=h*3600" +
  42.     "000;var m=Math.ceil(td/60000);if(r){if(h>0){if(m>0){if(r=='u')h++;m" +
  43.     "=0;}}if(d>0){if((h>0)||(m>0)){if(r=='u')d++;h=0;m=0;}}}d=s.lPS(d,p," +
  44.     "0);h=s.lPS(h,p,0);m=s.lPS(m,p,0);return d+':'+h+':'+m;");
  45. // end getTimeDifference
  46.  
  47. /**
  48.   * Plugin: getTimeBetweenEvents
  49.             gets the difference in time between two events
  50.   * Acronym Media 2014.10.21
  51.   * Dependencies: getTimeDifference, lPS, inList, c_r, c_w
  52.   * str  e1 1st event. If falsey value passed, default is first hit
  53.             ever or next hit after e2 if ro==false
  54.   * str  e2 2nd event
  55.   * str  c cookie to store timestamps (default: s_gtbe)
  56.   * int  d cookie expiration, in days
  57.   * str  rc control getTimeDiff rounding
  58.   *         d rounds down
  59.             u rounds up
  60.        (default/any other value) returns exact
  61.   * bool ro true: return 'repeat' on 2nd+ e2.
  62.             false (default) wipe cookie after e2 to always return time
  63.   * return
  64.       string '' if no event triggered
  65.       string [dd:hh:mm] on e2
  66.       string 'repeat' after 2nd+ e2 if ro==true
  67.   */
  68. s.getTimeBetweenEvents = new Function("e1", "e2", "c", "d", "rc", "ro", "" +
  69.     "var s=this,rc=(rc||''),r='repeat',x='',ev=(s.events||''),c=(c||'s_g" +
  70.     "tbe'),ro=(ro||false),d=(Number(d)||false);var o=s.c_r(c).split('|')" +
  71.     ";var t=(new Date()).getTime();if(d){x=new Date;x.setTime(x.getTime(" +
  72.     ")+1000*60*60*24*d);}if(!e2)return '';if(!e1||s.inList(e1,ev,',',':'" +
  73.     ")){if(!o[0]){s.c_w(c,t,x);}}if(s.inList(e2,ev,',',':')){if(o[0]){if" +
  74.     "(o[1]==r)return r;if(ro)s.c_w(c,o[0]+'|'+r,x);else s.c_w(c,'',-100)" +
  75.     ";return s.getTimeDifference(o,t,rc);}else{return 'error: e2 before " +
  76.     "e1';}}return ''");
Advertisement
RAW Paste Data Copied
Advertisement