/
scroll_tracking.php
53 lines (44 loc) · 2.18 KB
/
scroll_tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* PHP wrapper of a slightly modified version of the scroll tracking
* javascript by Justin Cutroni, suitable for use in WordPress.
*
* The javascript needs to be called scroll_tracking.js and to reside
* in the URI specified in the named constant ANALYTICS_URI.
*
* Created by Guido W. Pettinari on 23.12.2015.
* Part of WordPress Analytics:
* https://github.com/coccoinomane/wordpress_analytics
*/
function wpan_scroll_tracking () {
/* Extract the scroll tracking options from the database */
$options = wpan_get_options ();
$ga_tracker = isset ( $options ['tracker_name'] ) ? $options ['tracker_name'] : '';
$pixel_threshold = isset ( $options['pixel_threshold'] ) ? $options['pixel_threshold'] : '';
$time_threshold = isset ( $options['time_threshold'] ) ? $options['time_threshold'] : '';
$custom_dimension_scroll_tracking = isset ( $options['custom_dimension_scroll_tracking'] ) ? $options['custom_dimension_scroll_tracking'] : '';
$debug = isset ( $options['debug'] ) ? $options['debug'] : '';
/* Script path & url */
$script_path = WPAN_PLUGIN_DIR . 'js/scroll_tracking.js';
$script_url = WPAN_PLUGIN_URL . 'js/scroll_tracking.js';
/* Add the timestamp as a query string to the script, in order to reload
automatically the script when it is changed rather than using the cached
version (see http://stackoverflow.com/a/14536240/2972183) */
$script_versioned = $script_url . '?ver=' . filemtime($script_path);
/* Load the imagesLoaded javascript library to ensure that scroll
tracking works properly with image-rich pages.
TODO: we should use wp_enqueue() here. */
echo "<script src='https://npmcdn.com/imagesloaded@4.1/imagesloaded.pkgd.js' defer='defer'></script>\n";
/* Load the script.
TODO: we should use wp_enqueue() here. */
echo "<script src='$script_versioned' "
. "gaTracker='$ga_tracker' "
. "timeThreshold='$time_threshold' "
. "pixelThreshold='$pixel_threshold' "
. "customDimension='$custom_dimension_scroll_tracking' "
. "debug='$debug' "
. "defer='defer'"
. "> "
. "</script>\n";
}
?>