Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * ChartBeat Scroll History
  5. */
  6.  
  7. # Retrieve the data
  8. $response = '{"data":{"stats":{"title":"Microsoft to cut an additional 2,850 jobs | INFORUM","scroll":{"hist":[2,2,3,5,5,2,1,0,0,0],"avg":2.95,"median":3}}}}';
  9. $scroll_hist = json_decode( $response, true )['data']['stats']['scroll']['hist'];
  10.  
  11. # Total the Concurrent Users
  12. $total = '0';
  13. foreach ( $scroll_hist as $hist ) {
  14. $total = ($total + $hist);
  15. }
  16.  
  17. # Create an array of the cumulative values
  18. $scroll_depth_array = array();
  19. $i = '0';
  20. foreach ( $scroll_hist as $hist ) {
  21. if ( '0' == $i ) {
  22. $scroll_depth_array[] = $hist;
  23. $i++;
  24. } else {
  25. $index_offset = ($i - 1);
  26. $scroll_depth_array[] = ( $hist + $scroll_depth_array[ $index_offset ] );
  27. $i++;
  28. }
  29. }
  30.  
  31. # Compute the scroll depth percentages
  32. $scroll_depth_percents = array();
  33. foreach ( $scroll_depth_array as $depth ) {
  34. $scroll_depth_percents[] = ( ( $total - $depth ) / $total );
  35. }
  36.  
  37. echo '<pre>';
  38. print_r($scroll_depth_percents);
  39.  
  40. /** Returns:
  41. Array
  42. (
  43. [0] => 0.9 // 90% of readers read until here. (10% of the screen height)
  44. [1] => 0.8 // 80% of readers read until here. (20% of the screen height)
  45. [2] => 0.65 // 65% of readers read until here. (30% of the screen height)
  46. [3] => 0.4 // 40% of readers read until here. (40% of the screen height)
  47. [4] => 0.15 // 15% of readers read until here. (50% of the screen height)
  48. [5] => 0.05 // 5% of readers read until here. (60% of the screen height)
  49. [6] => 0 // 0% of readers read until here. (70% of the screen height)
  50. [7] => 0 // 0% of readers read until here. (80% of the screen height)
  51. [8] => 0 // 0% of readers read until here. (90% of the screen height)
  52. [9] => 0 // 0% of readers read until here. (100% of the screen height)
  53. )
  54. */
  55.  
  56. /* Misc Notes:
  57. the vast majority of readers’ collective time is spent below the fold (the typical height of a browser window, about 700 pixels)
  58.  
  59. scroll [h]: Number of concurrents that made it down a certain way of the page--histogram buckets are broken down into 10% intervals of the page.
  60. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement