PluginProbe
Plausible Analytics / trunk
Plausible Analytics vtrunk
2.6.1 2.6.0 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 52 releases
plausible-analytics / src / Assets.php

Assets.php in Plausible Analytics trunk, at src/Assets.php

209 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plausible Analytics | Assets
4 */
5
6 namespace Plausible\Analytics\WP;
7
8 class Assets {
9 /**
10 * Build class.
11 *
12 * @param bool $init
13 */
14 public function __construct() {
15 $this->init();
16 }
17
18 /**
19 * Action/filter hooks.
20 *
21 * @return void
22 */
23 private function init() {
24 add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_main_script' ], 1 );
25 add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_cloaked_affiliate_links_assets' ], 11 );
26 add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_four_o_four_script' ], 11 );
27 add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_query_params_script' ], 11 );
28 add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_search_queries_script' ], 11 );
29 }
30
31 /**
32 * Enqueue the cloaked affiliate links assets if the option is enabled.
33 *
34 * @return void
35 */
36 public function maybe_enqueue_cloaked_affiliate_links_assets() {
37 if ( Helpers::is_cloaked_affiliate_links_enabled() && Helpers::main_script_is_registered() ) {
38 wp_enqueue_script(
39 'plausible-affiliate-links',
40 PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js',
41 [ 'plausible-analytics' ],
42 filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js' ),
43 [ 'in_footer' => true ],
44 );
45
46 $affiliate_links = Helpers::get_settings()['affiliate_links'] ?? [];
47
48 wp_add_inline_script( 'plausible-affiliate-links', 'const plausibleAffiliateLinks = ' . wp_json_encode( $affiliate_links ) . ';', 'before' );
49 }
50 }
51
52 /**
53 * Enqueue the 404 script if the option is enabled.
54 *
55 * @return void
56 */
57 public function maybe_enqueue_four_o_four_script() {
58 $is_404 = apply_filters( 'plausible_analytics_is_404', is_404() );
59
60 if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::FOUR_O_FOUR ) && $is_404 && Helpers::main_script_is_registered() ) {
61 $data = wp_json_encode(
62 [
63 'props' => [
64 'path' => 'document.location.pathname',
65 ],
66 ]
67 );
68
69 /**
70 * document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
71 */
72 $data = str_replace( '"document.location.pathname"', 'document.location.pathname', $data );
73 $event_name = EnhancedMeasurements::FOUR_O_FOUR;
74
75 wp_add_inline_script(
76 'plausible-analytics',
77 "document.addEventListener('DOMContentLoaded', () => { plausible( $event_name, $data ); });"
78 );
79 }
80 }
81
82 /**
83 * Register main JS if this user should be tracked.
84 *
85 * @since 1.0.0
86 * @access public
87 * @return void
88 * @throws \Exception
89 */
90 public function maybe_enqueue_main_script() {
91 $settings = Helpers::get_settings();
92 $user_role = Helpers::get_user_role();
93 $url = $this->get_js_url( true );
94
95 if ( ! $url ) {
96 echo '<!-- ' . __( 'Please enter your plugin token to start using Plausible Analytics.', 'plausible-analytics' ) . " -->\n";
97
98 return;
99 }
100
101 /**
102 * Enqueue the JS container for this domain.
103 */
104 wp_register_script( 'plausible-analytics', $url, [], null, apply_filters( 'plausible_load_js_in_footer', false ) );
105
106 /**
107 * Bail if tracked_user_roles is empty (which means no roles should be tracked) or if the current role should not be tracked.
108 */
109 if ( ( ! empty( $user_role ) && ! isset( $settings['tracked_user_roles'] ) ) || ( ! empty( $user_role ) && ! in_array( $user_role, $settings['tracked_user_roles'], true ) ) ) {
110 return; // @codeCoverageIgnore
111 }
112
113 wp_enqueue_script( 'plausible-analytics' );
114
115 $script = 'window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};';
116 $options = wp_json_encode( apply_filters( 'plausible_analytics_init_options', [] ) );
117 // transformRequest and customProperties can contain a JS function.
118 $options = preg_replace(
119 '/"(transformRequest|customProperties)"\s*:\s*"(\(\)\s*=>\s*{[^}]*})"/',
120 '"$1": $2',
121 $options
122 );
123 $script .= "\nplausible.init($options);";
124
125 wp_add_inline_script( 'plausible-analytics', $script );
126
127 // This action allows you to add your own custom scripts!
128 do_action( 'plausible_analytics_after_register_assets', $settings );
129 }
130
131 /**
132 * This seam keeps our code testable.
133 *
134 * @param bool $local
135 *
136 * @return string
137 * @throws \Exception
138 *
139 * @codeCoverageIgnore Because Helpers are tested elsewhere.
140 */
141 protected function get_js_url( bool $local = false ) {
142 return Helpers::get_js_url( $local );
143 }
144
145 /**
146 * Enqueue the Query Params script if the option is enabled.
147 *
148 * @return void
149 */
150 public function maybe_enqueue_query_params_script() {
151 if ( Helpers::is_query_params_enabled() && Helpers::main_script_is_registered() ) {
152 $props = [];
153 $query_params = Helpers::get_settings()['query_params'] ?? [];
154
155 foreach ( $query_params as $query_param ) {
156 if ( isset( $_REQUEST[ $query_param ] ) ) {
157 $props[ $query_param ] = $_REQUEST[ $query_param ];
158 }
159 }
160
161 if ( ! empty( $props ) ) {
162 $data = wp_json_encode(
163 [
164 'props' => $props,
165 ]
166 );
167
168 $script = "plausible('WP Query Parameters', $data );";
169
170 wp_add_inline_script(
171 'plausible-analytics',
172 "document.addEventListener('DOMContentLoaded', () => {\n$script\n});"
173 );
174 }
175 }
176 }
177
178 /**
179 * Enqueue the Search Queries script if the option is enabled.
180 *
181 * @return void
182 */
183 public function maybe_enqueue_search_queries_script() {
184 $is_search = apply_filters( 'plausible_analytics_is_search', is_search() );
185
186 if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::SEARCH_QUERIES ) && $is_search && Helpers::main_script_is_registered() ) {
187 global $wp_query;
188
189 $search_source = isset( $_REQUEST['search_source'] ) ? sanitize_text_field( $_REQUEST['search_source'] ) : wp_get_referer();
190 $data = wp_json_encode(
191 [
192 'props' => [
193 // convert queries to lowercase and remove trailing whitespace to ensure the same terms are grouped together
194 'search_query' => strtolower( trim( get_search_query() ) ),
195 'result_count' => $wp_query->found_posts,
196 'search_source' => $search_source,
197 ],
198 ]
199 );
200 $script = "plausible('WP Search Queries', $data );";
201
202 wp_add_inline_script(
203 'plausible-analytics',
204 "document.addEventListener('DOMContentLoaded', () => {\n$script\n});"
205 );
206 }
207 }
208 }
209