@@ -28,6 +28,55 @@ private function init() {
2828 add_action ( 'wp_enqueue_scripts ' , [ $ this , 'maybe_enqueue_search_queries_script ' ], 11 );
2929 }
3030
31+ /**
32+ * Enqueue 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 ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::CLOAKED_AFFILIATE_LINKS ) && 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+ );
44+
45+ $ affiliate_links = Helpers::get_settings ()['affiliate_links ' ] ?? [];
46+
47+ wp_add_inline_script ( 'plausible-affiliate-links ' , 'const plausibleAffiliateLinks = ' . wp_json_encode ( $ affiliate_links ) . '; ' , 'before ' );
48+ }
49+ }
50+
51+ /**
52+ * Enqueue 404 script if the option is enabled.
53+ *
54+ * @return void
55+ */
56+ public function maybe_enqueue_four_o_four_script () {
57+ $ is_404 = apply_filters ( 'plausible_analytics_is_404 ' , is_404 () );
58+
59+ if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::FOUR_O_FOUR ) && $ is_404 && Helpers::main_script_is_registered () ) {
60+ $ data = wp_json_encode (
61+ [
62+ 'props ' => [
63+ 'path ' => 'document.location.pathname ' ,
64+ ],
65+ ]
66+ );
67+
68+ /**
69+ * document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
70+ */
71+ $ data = str_replace ( '"document.location.pathname" ' , 'document.location.pathname ' , $ data );
72+ $ event_name = EnhancedMeasurements::FOUR_O_FOUR ;
73+
74+ wp_add_inline_script (
75+ 'plausible-analytics ' ,
76+ "document.addEventListener('DOMContentLoaded', () => { plausible( $ event_name, $ data ); }); "
77+ );
78+ }
79+ }
3180
3281 /**
3382 * Register main JS if this user should be tracked.
@@ -40,11 +89,18 @@ private function init() {
4089 public function maybe_enqueue_main_script () {
4190 $ settings = Helpers::get_settings ();
4291 $ user_role = Helpers::get_user_role ();
92+ $ url = $ this ->get_js_url ( true );
93+
94+ if ( ! $ url ) {
95+ echo '<!-- ' . __ ( 'Please enter your plugin token to start using Plausible Analytics. ' , 'plausible-analytics ' ) . " --> \n" ;
96+
97+ return ;
98+ }
4399
44100 /**
45101 * This is a dummy script that will allow us to attach inline scripts further down the line.
46102 */
47- wp_register_script ( 'plausible-analytics ' , $ this -> get_js_url ( true ) , [], null , apply_filters ( 'plausible_load_js_in_footer ' , false ) );
103+ wp_register_script ( 'plausible-analytics ' , $ url , [], null , apply_filters ( 'plausible_load_js_in_footer ' , false ) );
48104
49105 /**
50106 * Bail if tracked_user_roles is empty (which means no roles should be tracked) or if the current role should not be tracked.
@@ -85,63 +141,13 @@ protected function get_js_url( bool $local = false ) {
85141 return Helpers::get_js_url ( $ local );
86142 }
87143
88- /**
89- * Enqueue cloaked affiliate links assets if the option is enabled.
90- *
91- * @return void
92- */
93- public function maybe_enqueue_cloaked_affiliate_links_assets () {
94- if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::CLOAKED_AFFILIATE_LINKS ) ) {
95- wp_enqueue_script (
96- 'plausible-affiliate-links ' ,
97- PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js ' ,
98- [ 'plausible-analytics ' ],
99- filemtime ( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js ' ),
100- );
101-
102- $ affiliate_links = Helpers::get_settings ()['affiliate_links ' ] ?? [];
103-
104- wp_add_inline_script ( 'plausible-affiliate-links ' , 'const plausibleAffiliateLinks = ' . wp_json_encode ( $ affiliate_links ) . '; ' , 'before ' );
105- }
106- }
107-
108- /**
109- * Enqueue 404 script if the option is enabled.
110- *
111- * @return void
112- */
113- public function maybe_enqueue_four_o_four_script () {
114- $ is_404 = apply_filters ( 'plausible_analytics_is_404 ' , is_404 () );
115-
116- if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::FOUR_O_FOUR ) && $ is_404 ) {
117- $ data = wp_json_encode (
118- [
119- 'props ' => [
120- 'path ' => 'document.location.pathname ' ,
121- ],
122- ]
123- );
124-
125- /**
126- * document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
127- */
128- $ data = str_replace ( '"document.location.pathname" ' , 'document.location.pathname ' , $ data );
129- $ event_name = EnhancedMeasurements::FOUR_O_FOUR ;
130-
131- wp_add_inline_script (
132- 'plausible-analytics ' ,
133- "document.addEventListener('DOMContentLoaded', () => { plausible( $ event_name, $ data ); }); "
134- );
135- }
136- }
137-
138144 /**
139145 * Enqueue Query Params script if the option is enabled.
140146 *
141147 * @return void
142148 */
143149 public function maybe_enqueue_query_params_script () {
144- if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::QUERY_PARAMS ) ) {
150+ if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::QUERY_PARAMS ) && Helpers:: main_script_is_registered () ) {
145151 $ query_params = Helpers::get_settings ()['query_params ' ] ?? [];
146152 $ props = [];
147153
@@ -176,7 +182,7 @@ public function maybe_enqueue_query_params_script() {
176182 public function maybe_enqueue_search_queries_script () {
177183 $ is_search = apply_filters ( 'plausible_analytics_is_search ' , is_search () );
178184
179- if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::SEARCH_QUERIES ) && $ is_search ) {
185+ if ( EnhancedMeasurements::is_enabled ( EnhancedMeasurements::SEARCH_QUERIES ) && $ is_search && Helpers:: main_script_is_registered () ) {
180186 global $ wp_query ;
181187
182188 $ search_source = isset ( $ _REQUEST ['search_source ' ] ) ? sanitize_text_field ( $ _REQUEST ['search_source ' ] ) : wp_get_referer ();
0 commit comments