plausible/wordpress

Enhanced measurements broken if self-host

Closed this issue ยท 28 comments

There is a current Bug with the "Enhanced measurements" toggle Buttons.
If you use a Self-Host domain the is_list value will be empty instead of 1.
This is because the disabled information Banner will be added as a hook to the element Array.
hook is not ignored by the check if the Array is a list.
Because of this the Toggle Buttons will no longer work.
In addition the enhanced_measurements entry in the Database changes from ['...','...'] to 'on'

Thanks for the detailed report. I'll look into it tomorrow!

For your convenience, the Check that @Dominic1DL is referring to are these lines in src/Admin/Settings/API.php. Specifically, line 587.

Of note is, that once the settings array in the database has been set to "on", if you force the is_list parameter on the checkboxes to be truthy (via the browser's developer console), then the ajax handler errors out in src/Ajax.php. This is because the in_array check uses the value of the settings array with the group name as the key, which in this circumstance is the string "on", as the second parameter. This causes an error since the second parameter is expected to be an array. Here's the stack trace for that:

PHP message: PHP Fatal error:  Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, string given in /var/www/clients/clientX/webY/web/www/wp-content/plugins/plausible-analytics/src/Ajax.php:132
Stack trace:
#0 /var/www/clients/clientX/webY/web/www/wp-content/plugins/plausible-analytics/src/Ajax.php(132): in_array()
#1 /var/www/clients/clientX/webY/web/www/wp-includes/class-wp-hook.php(324): Plausible\Analytics\WP\Ajax->toggle_option()
#2 /var/www/clients/clientX/webY/web/www/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#3 /var/www/clients/clientX/webY/web/www/wp-includes/plugin.php(517): WP_Hook->do_action()
#4 /var/www/clients/clientX/webY/web/www/wp-admin/admin-ajax.php(188): do_action()
#5 {main}
  thrown in /var/www/clients/clientX/webY/web/www/wp-content/plugins/plausible-analytics/src/Ajax.php on line 132

(Line numbers are probably different because of comparing the development version with the release version)

Thanks! I'm currently hit by a flu, guys. So, hopefully I'll be able to get back at it tomorrow.

This beta contains the fix for this. Mind you that running this beta requires you to updated Plausible Analytics CE to v2.1.0-rc.0 as an API token is required for self-hosted instances in this release as well.

Hi @Dan0sz,

I gave it a try: when I update the plugin (the provided beta version), something seems odd to me. I am on v2.1.0-rc.0. I can see the screen of the Plausible settings in WordPress changes after the update.

Originally it showed me at the section: "Connect your website with Plausible Analytics":
"This option is disabled, because the Domain Name setting is enabled under Self-Hosted settings."

Now with the updated beta version it shows me:
"Please create an API token and insert it into the API token field above."

When I click "create an API token" it takes me to plausible.io and not myplausiblename.mydomain.com. That does not seem right. Even though I am able to create an API token at Plausible-cloud and even get the status "connected". But I assume now it's connected to the cloud version of Plausible (which I previously used). I think "create an API token" should open https:/myplausiblename.mydomain.com/websitedomain.com/settings/integrations but even though I am on v2.1.0-rc.0 I don't see the desired settings there for "Plugins API Tokens"

So to me it looks there are many things mixed up. Maybe you can have a look at this and let me know.

I am able to go to myplausiblename.mydomain.com and go to the Account settings to create an API key. But I think that's something compeltely different, and it's not working.

Right now I am in a very confused state and situation, as I think it made the entire situation worse unfortunately.

Also, the actual issue I described here https://wordpress.org/support/topic/cant-enable-404-error-outbound-links-file-downloads-anymore/ seems not to be solved either, e.g. I tried to enable "404 error pages" and it's not keeping the setting, I goes back to being disabled once I refresh the page.

Yes, you're right about that. It should point to your self-hosted domain. Thanks for notifying me!

As you described, you should still be able to create the API token yourself. That's @ukutaht's territory, though :-)

this is the link that we link from the plugin when cloud is used: https://plausible.io/yoursite.com/settings/integrations?new_token=WordPress

same type of link should work with v2.1.0-rc.0 (note the ?new_token=WordPress at the end, without it it won't work). so something like:

https://yoursubdomain.yourdomain.com/yoursite.com/settings/integrations?new_token=WordPress

I can try to create it myself by modifying the link. And to my surprise the first part works. But that's not it yet.

  1. When I go to myplausiblename.mydomain.com and pick a domain and see for it's settings โ†’ integration โ†’ the sectin "Plugins API Tokens" is misisng
  2. When I modify the link as you suggested manually then I'll replace https://plausible.io/websitedomain.com/settings/integrations?new_token=WordPress with https://myplausiblename.mydomain.com/websitedomain.com/settings/integrations?new_token=WordPress and to my surprise I can create an API token. After I have done that, I can go to the settings of the site in myplausiblename.mydomain.com and suddenly the section "Plugins API Tokens" is there

However, nothing of that matters. Because once I try to add the API token it says:
"Oops! The API token you used is invalid. Please click here to generate a new token."

@Dan0sz both the links to create the API token and the link to where we validate the inserted token need to be changed in the plugin when self-hosting is enabled

Right. Of course, will work that in tomorrow!

@Dan0sz Thanks for letting us know. It's a public holiday tomorrow, so earliest we'll get to check it out will probably be Thursday. ace34a8 should fix the initial problem, however I don't see a fix for if the settings value is already messed up, i.e. if you toggled any of the switches before the update and the settings value has been set to "on".


@raabmar

I tried to enable "404 error pages" and it's not keeping the setting, I goes back to being disabled once I refresh the page.

This might be related to what I described above and in my previous comment. Using version 2.0.8 (and presumably earlier versions as well) with a self-hosted Plausible instance, the plugin messes up some setting if you toggle any of the switches in the enhanced measurements section. Using the beta you might be getting an error message and not notice it. I can't test the beta version right now but with the 2.0.8 version I get a little error popup in the upper right-hand corner saying "undefined" and my browser console informs me of a 500 internal server error. Depending on your web server configuration you might not get these and only get a PHP warning in the server logs, though I'm not 100% on this one.

If possible, could you please check that for me?


If these hold true, then yes technically the bug is fixed. But for people who have used the plugin before and ran into the same issue, the in_array check when submitting the toggle status will error out because of incompatible types. In that sense I don't think the bug is fixed, because it still prevents enabling the enhanced measurements.

@icewindow @Dan0sz I checked and when I try to toggle the setting, e.g. 404 error pages, it throws an 500 error, shown in the browser console

POST https://www.mywebsite.com/wp-admin/admin-ajax.php 500 (Internal Server Error)

Can I just uninstall the plugin completely and reinstall it? Or is that not on the end of WordPress, but the self-hosted plausible itself?

Ah, right. I'll include a migration script to make sure it's fixed.

Thanks, I am traveling right now but the minutes in between on the laptop I can try to test it once you are ready

@raabmar

Can I just uninstall the plugin completely and reinstall it? Or is that not on the end of WordPress, but the self-hosted plausible itself?

The Error comes from WordPress itself, because the plugin tries to check it's settings and because the wrong value that got previously saved is still there, some check fails and causes the 500 server error. I'm not sure if uninstalling the plugin would fix that because I'm not sure if the plugin deletes it's settings upon being uninstalled.

Best thing to do is probably to wait for the migration script being added and then try again.
You could manually delete the enhanced_measurements entry from your wp_options database table or use the WordPress CLI to remove the option, but if you are unfamiliar with either I'd recommend against it. You can mess up your WordPress if you make a mistake (always have a backup!). Though the CLI is somewhat safe, but you need access to the server's console in order to use it, which you might not have.

Creating a new beta as we speak, guys.

Please give this beta try!

Can you explain please what it should do? It should fix the links for creating the API token and the validation right?

But not include what you called migration script, correct?

I can't risk to mess with another site so I better ask beforehand (I don't run plausible on staging sites, only live, so each try is also a risk for me)

This beta fixes the Create the API token link, and other related links which should point to the self-hosted instance if enabled.

It also includes the migration script.

I just tested it, and I feel there is progress, and then there are steps backwards as well. To sum it up - I had to disable the plugin now entirely!

It turns out, when I logged back in to the site, I was not able to open any page in wp-admin, it always automatically redirected me to the plausible settings page. Same when I tried in incognito mode. This happened before updating to the latest beta from #202 (comment)

I renamed the entire plugin folder via FTP, otherwise I was not even able to navigate in the WordPress backend. Then I uploaded the latest beta from #202 (comment)

The initial result: When I request "create an API token" it does open the self-hosted plausible now โœ… I can create a token as expected โœ… but the token does not work โ†’ โŒ "Oops! The API token you used is invalid. Please click here to generate a new token."

Note: in the error message it says "click here" to generate a new token, and there is also no link behind it at all. โŒ

Then I try to refresh the page and then it shows me out of a sudden "connected", so now it suddenly does work. โœ…

Next I test the toggles for 404 error, outbound links โ†’ those did work now โœ…

But then again, I can't go anywhere else in WordPress - I am always redirected to https://www.mywebsite.com/wp-admin/options-general.php?page=plausible_analytics#welcome_slide โŒ

I had to disable the plugin via FTP.

On another note - is there a way to go back now?
I lost track on how the Plausible self-hosted version and the WordPress plugin version must be tied. I am just not sure if I am even able to go back.

I have the Plausibel plugin now disabled for a few days. I would like to enable it again, but I can't because of the issues desribed in my previous comment.

But with all the trials and attempts I am not sure anymore if I could just somehow get the previous WordPress plugin version, that comes with the issue from this entire ticket (e.g. 404 error tracking), but at least I could just track normal visitors

Now I am in some state in between and feel I can't move forward, and can't move backwards

Hello @Dan0sz

do you have by chance any update on this? It's becoming problematic for me, and I really would just like to use Plausible again, which aI can't know anymore - I can not fully understand what the first and the second beta were doing in the DB, so I can't just go back - or at least not until you tell me it's fine

Can you please try to give some update on that case?

@raabmar,

Sorry for the delay.

I was unable to reproduce the issue you mentioned of your admin area being locked up, but when digging through the code I found something that might be causing your issue. So, I created another beta (yes, I know) that only includes a fix for that. If you're willing (and I fully understand if you're a bit hesitant right now ๐Ÿ˜…), you can give this beta a try.

@Dan0sz I did a quick test. This latest beta does solve the redirect issue in wp-admin. But now I again get "Oops! The API token you used is invalid. Please click here to generate a new token." even though I am generating the API token from the self hosted environment.

So it looks like now the validation part is broken again.

And something else - when I hover over "Plausible Analytics" in the admin bar in WordPress, it's not showing the dropdown anymore. I can still directly access the URL /wp-admin/admin.php?page=plausible_analytics but I am surprised why this is broken now.

Not sure if GIF works in here - to show it visualy. The first example shows the public 2.0.8. The second example shows the beta version from #202 (comment)

This latest issue didn't show with the beta version from #202 (comment). There was a version where the new API token validation did work already

2024-05-11_005972

@Dan0sz Do you have by chance any update on this? I know I ask and request a lot, but those betas cost me a lot of time as you know, and I still did not manage to get into a stable situation since this all started. Seems it requires a mix of all provided beta versions to make it work

The admin bar issue is considered a low priority issue at the moment, and is present already on our tracker.

After extensively testing your issue, I found that the API client wrapper didn't respect some refactors I did earlier yet, which resulted in plausible.io's API still being hit, instead of your self-hosted environment. That's what was causing the "Oops! Your token is invalid" message.

This beta resolves it, and works fully with the latest RC of Plausible CE.

Can't do a proper testing right now, but the one site I performed all the beta went anyway through a lot so I just updated there and I can confirm the token validation did work now.

I also quickly disabled settings about 404 error pages (that's how it all started initially), page refresh, enabled 404 error pages again, page refresh โ†’ confirm the settings are stored now on that site

Disable menu bar in the settings works, so I can at least disable it therre so that it doesn't disturb users when it's non functional at the moment

I hope others can test as well, I am not really ready to update it on more sites just yet, as I have Plausible only running on live sites and not on dev sites

Thanks for the hard work