Skip to content

Commit 2db4fc1

Browse files
authored
Merge pull request #298 from plausible/disable_tracking_without_token
Improved: don't load JS tracking code if no API token is entered
2 parents ceda3b9 + e2f2a94 commit 2db4fc1

5 files changed

Lines changed: 275 additions & 238 deletions

File tree

src/Assets.php

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/Cron.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ private function maybe_download() {
5757
$remote = Helpers::get_js_url();
5858
$local = Helpers::get_js_path();
5959

60+
if ( ! $remote || ! $local ) {
61+
return false;
62+
}
63+
6064
return $this->download_file( $remote, $local );
6165
}
6266

6367
/**
6468
* Downloads a remote file to this server.
6569
*
66-
* @param string $local_file Absolute path to where to store the $remote_file.
70+
* @since 1.3.0
71+
*
6772
* @param string $remote_file Full URL to file to download.
6873
*
74+
* @param string $local_file Absolute path to where to store the $remote_file.
75+
*
6976
* @return bool True when successful. False if it fails.
70-
* @throws Exception
7177
* @throws InvalidArgument
7278
*
73-
* @since 1.3.0
79+
* @throws Exception
7480
*/
7581
private function download_file( $remote_file, $local_file ) {
7682
$file_contents = wp_remote_get( $remote_file );

0 commit comments

Comments
 (0)