plausible / wordpress

Plausible Analytics WordPress plugin
https://plausible.io/wordpress-analytics-plugin
MIT License
50 stars 26 forks source link

Cannot set "domain name" when using self-hosted mode #177

Closed deltamualpha closed 2 months ago

deltamualpha commented 7 months ago

Newly-installed plugin, latest version of both it and wordpress. Activated "self-hosted" mode, and see this being injected into the header:

<script defer data-domain='' data-api='https://<snip>/api/event' src="https://<snip>/js/plausible.outbound-links.js?ver=2.0.0" id="plausible"></script>

Tracing through the code, it looks like it's supposed to be filling in the data-domain from the plugin settings -- but those settings can only be updated if using the cloud, hosted version of Plausible.

deltamualpha commented 7 months ago

Looks like a workaround is to:

  1. delete the self-hosted domain from the settings and save.
  2. add the hostname and an arbitrary random string as the "API key" and save.
  3. go back and re-add the self-hosted domain to settings and save.

Embedded analytic dashboard also isn't working, but not sure why that is yet.

Edit: this workaround no longer works as of the 2.0.5 release, which attempts to validate the API key to Plausible's servers on save.

Dan0sz commented 7 months ago

I can confirm this is a bug, which'll be fixed in 2.0.3, which is about to be released in 3... 2... 1...

Embedded analytic dashboard also isn't working, but not sure why that is yet.

Same for this one, will be released in 2.0.3.

Unkn0wn0x commented 6 months ago

Still does not work with version 2.0.7. The data-domain attribute is not added to the <script> tag unless you provide a valid access token for the plausible hosted setting. Any random access token will not be accepted, as it's directly checked for validity.

Dan0sz commented 6 months ago

Hmm, you're right. The API validation blocks saving the domain_name. I just created a fix for this. You can get the prerelease here.

tenfourty commented 6 months ago

I have the same issue, and I tried this prerelease, and it didn't work. I've downgraded to 1.3.6, which works well with my self-hosted setup (the backend is on the new Plausible Community Edition).

Unkn0wn0x commented 6 months ago

Does not work on the pre-release as @tenfourty mentioned, I'm still on the latest v1.3.x where everything works fine for me.

Dan0sz commented 6 months ago

@Unkn0wn0x and @tenfourty, does the Domain Name option under the Self-Hosted tab contain a value in your configuration? Because that's how the plugin (the prerelease) checks if a self-hosted setup is being used, and if so, it doesn't attempt to validate the API token.

tenfourty commented 6 months ago

Yes, I have a value in the field, and I'm getting the error, AND I can't enter an API key.

If I remove the self-hosted info, I can enter an API key.

I don't know if this is related or if I should raise it as a separate issue, but the link to create an API key is hard-coded to plausible.io and doesn't consider the self-hosted URL.

ccoenen commented 5 months ago

This is still an issue in 2.0.8, should we re-open this issue or would you like me to open another one?

I have a domain set on the "Settings" tab (and no API token). And I have the plausible domain set in the "Self-Hosted" tab. And still the data-domain comes up empty. This happens to me on two separate WordPress blogs.

ccoenen commented 5 months ago

investigating further, I can pinpoint the problem down to this

https://github.com/plausible/wordpress/blob/18bc986f75baffe97fdf7868afce7193b0ef056e/src/Filters.php#L49

I added some echos for simple debugging here, and $domain_name just comes up empty for me.

grafik

grafik

if $domain_name worked, the first comment in the screenshot above would also contain something. But only the helper works correctly.

ccoenen commented 5 months ago

a var_dump in the same place also confirms that $settings['domain_name'] contains an empty string. Please don't ask me why the get_settings() does not work here when get_domain() (which also calls get_settings() internally) works. This is not my code. I only point out what works and what doesn't.

ccoenen commented 5 months ago

Replacing

https://github.com/plausible/wordpress/blob/18bc986f75baffe97fdf7868afce7193b0ef056e/src/Filters.php#L49

-       $domain_name = esc_html( $settings[ 'domain_name' ] ); 
+       $domain_name = esc_html( Helpers::get_domain() );

works fine for me and actually solves the problem.

Dan0sz commented 3 months ago

@ccoenen and @deltamualpha, can you confirm if you're still experiencing this issue in v2.0.9? This release contains several improvements on the self-hosters (Plausible CE) side.

robogao-net commented 3 months ago

I just installed the latest version v2.0.9 and encountered the same issue.

ccoenen commented 3 months ago

yes, 2.0.9 still has the data-domain="" attribute empty.

empty data-domain attribute

This then leads to this request failing with the error message that the data-domain needs to be filled.

failing outbound links

I should say "again" has it empty, because this update nuked my fix that I had applied manually.

ccoenen commented 3 months ago

my fix from https://github.com/plausible/wordpress/issues/177#issuecomment-2049981781 still works. I just applied it and now it works fine again. It now needs to go in line 53:

https://github.com/plausible/wordpress/blob/f687aace2351fc3b849304741184b17ba4e4c7af/src/Filters.php#L53

-       $domain_name = esc_html( $settings[ 'domain_name' ] ); 
+       $domain_name = esc_html( Helpers::get_domain() );

This way, the data-domain attribute is filled correctly.

grafik

And that way, this request no longer fails with an HTTP 400 error.

grafik

And that way, I can get analytics on my self hosted plausible coming from my self hosted wordpress.

line goes up

Dan0sz commented 3 months ago

I investigated your fix @ccoenen and I think the reason why Helpers::get_domain() works in your case, is because if the domain_name option turns up as empty (for whatever reason) the Helpers function falls back to a regex to "guess" the domain name.

In short, implementing your fix will only work after handling #199.

JoAllg commented 3 months ago

We can confirm this issue. And just saying while interfering the domain name is great, there could be reasons to set a domain name in plausible that differes from the actual domain. Being able to optionally set a domain name would be preferable

Dan0sz commented 3 months ago

@ccoenen I've applied your fix in this commit. So, it'll be included in v2.1.0.

ccoenen commented 3 months ago

thanks :-)

echoblag commented 2 months ago

Howdy, I'm late to the party. Just posting for visiblity.

I wanted to confirm that installing 2.1.0-beta4 of the plugin resolved this for me.

Dan0sz commented 2 months ago

Thanks for confirming. Closing this.

ccoenen commented 1 week ago

Can confirm that this issue did not come up for me again. Thank you to everyone involved :-)