title: Javascript Timestamp Logic
body: <p>Hi I want to track “the time it takes a user to complete a purchase from a defined starting event” with the <code>_trackTiming</code> method in Google Analytics.</p>
  
  <p><strong>Method suggested by</strong> <a href="https://www.e-nor.com/blog/google-analytics/time-on-site-2-0-tracking-conversion-duration-in-google-analytics" rel="nofollow noreferrer">this blogpost</a>:</p>
  
  <ol>
  <li>Create a timestamp at the defined starting point. (Done)</li>
  <li>Save the timestamp in a cookie. (I have a cookie in <code>{{conversionDurationCookie}}</code> )</li>
  <li>Create a timestamp at the end of the conversion.</li>
  <li>Calculate the time duration from start to the purchase.</li>
  <li>Fire the <code>_trackTiming</code> method to track the duration in GA.</li>
  <li>Once this data is collected I will see the data in Content > Site Speed > User Timings in Google analytics.</li>
  </ol>
  
  <p><strong>Code 1-2</strong></p>
  
  <pre><code>//this is fired at the defined starting point
  var cookieName    = conversionDurationCookie;
  var cookieValue   = new Date().getTime();
  var cookieExpires = 1000 * 60 * 30;
  document.cookie   = cookieName + '=' + cookieValue + '; Expires=' + cookieExpires;
  </code></pre>
  
  <p><strong>Code 3-5:</strong></p>
  
  <pre><code>// this is fired at the conversion
  t2 = new Date().getTime();
  conversionDuration = (t2 – {{conversionDurationCookie}})
  ga("send", "timing", ["conversionDuration"], ["conversionDuration"], ["conversionDuration"]);
  </code></pre>
  
  <p></p>
  
  <p><strong>Code 3-5 (version 2):</strong></p>
  
  <pre><code>// this is fired at the conversion
  var t2 = new Date().getTime();
  function conversionDuration(t2 – {{conversionDurationCookie}}) {
  ga("send", "timing", ["conversionDuration"], ["conversionDuration"], ["conversionDuration"]);
  }
  </code></pre>
  
  <p><strong>Question 1-6</strong></p>
  
  <p>Before I do #6 do the logic look correct? Code 3-5 (version 2)? Correct format on functions and variables?</p>
  
  <p>Best
  //H</p>