Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: Update method for inserting JavaScript #3763

Closed
kraftbj opened this issue May 3, 2016 · 9 comments
Closed

Stats: Update method for inserting JavaScript #3763

kraftbj opened this issue May 3, 2016 · 9 comments
Labels
[Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. Hacktoberfest Support open source and earn a limited edition T-shirt! https://hacktoberfest.digitalocean.com/ [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Comments

@kraftbj
Copy link
Contributor

kraftbj commented May 3, 2016

We use a global var and a combo of wp_footer and shutdown to insert the stats js, initially as part of a workaround for sites that aren't using wp_footer. It's been long enough now that we can likely move to something more modern.

h/t fabacab/wp-sri#2 for the report

@kraftbj kraftbj added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. labels May 3, 2016
@kraftbj kraftbj added this to the Community milestone May 3, 2016
@samhotchkiss samhotchkiss modified the milestones: Community, Not Currently Planned Feb 10, 2017
@jeherve jeherve added the Hacktoberfest Support open source and earn a limited edition T-shirt! https://hacktoberfest.digitalocean.com/ label Oct 1, 2018
@rickcurran
Copy link
Contributor

rickcurran commented Aug 4, 2019

Hi, I'm trying to tackle this as a PHP-related "Good first bug". However, I'm not sure I fully understand the issues with the current method that is used to load the stats js file.

I understand that the stats JS is first attempted to be loaded using a call to wp_footer, and this is then followed by the workaround which is using the shutdown action to load it in using wp_head action, the latter only happening if the $rendered_stats_footer global variable is false.

What I'm trying to get my head around is what would be a 'more modern' solution to the issue. Is it safe to just presume that a theme will always usewp_footer? If so then a fairly simple update is to remove the workaround / fallback add_action( 'wp_head', 'stats_add_shutdown_action' ) and then remove the use of the $rendered_stats_footer global variable.
(If it's not safe to presume that a theme will always usewp_footer then I'm not sure what would be a more modern alternative way of doing it?)

I also wondered whether another modernisation would be to switch from the current method of printing the JavaScript block which sets all the _stq calls etc inline, instead using wp_enqueue_script to load in an external JavaScript file making use of wp_localize_script to set variables within this new external JS file. However, I wasn't sure if that is making it more complex than needed as well as adding yet another external JS file and corresponding loading overhead, printing inline is perhaps the most efficient method in this case?

An additional issue with using wp_enqueue_script is that there's currently no way to add the async or defer attributes so some further code would be required to apply these.

The code that renders the footer also outputs an AMP-specific footer with an 'amp-pixel' so this would have to be accommodated in any code change, I think this would still need to be output via wp_footer even if wp_enqueue_script was used for the stats JS.

I'd greatly appreciate any thoughts / suggestions anyone has!

@jeherve
Copy link
Member

jeherve commented Aug 5, 2019

Is it safe to just presume that a theme will always use wp_footer?

I think that by now, it may be a safe assumption to make. WordPress themes have changed a lot since this was originally implemented, and it has now become quite rare to see a theme that does not include the most basic WordPress hooks that are wp_head and wp_footer.

An additional issue with using wp_enqueue_script is that there's currently no way to add the async or defer attributes so some further code would be required to apply these.

Yes, I believe that's a blocker for using wp_enqueue_script right now. Until async is supported directly into wp_enqueue_script, I don't think we'll be able to use it to load stats.
The Stats script is loaded on enough sites and we don't really want to impact those sites' performance.

Related Trac ticket:
https://core.trac.wordpress.org/ticket/12009

@rickcurran
Copy link
Contributor

rickcurran commented Aug 5, 2019

Thanks @jeherve, so would an improvement for now be to get rid of the workaround / fallback add_action( 'wp_head', 'stats_add_shutdown_action' ), drop the use of the $rendered_stats_footer global variable but just keep it adding the stats JS via the existingwp_footer action?

@rickcurran
Copy link
Contributor

rickcurran commented Aug 5, 2019

I also just noticed that the Jetpack Amp support class (class.jetpack-amp-support.php) has a function add_stats_pixel which does a has_action check for the wp_footer action that is adding the stats JS, so this would need updating too if there was a switch to use wp_enqueue_script .

rickcurran added a commit to rickcurran/jetpack that referenced this issue Aug 5, 2019
After discussion on Github issue "Stats: Update method for inserting JavaScript Automattic#3763" it was considered that it was a safe assumption to make that a theme would always have a `wp_footer` action.

This update removes code which was used to provide a fallback / workaround in the case of themes which did not contain a `wp_footer` action.

The workaround consisted of setting a global variable `$rendered_stats_footer` which would get set to true if the `wp_footer` call was successful, if not successful then the WP Stats JS code would get set via a `wp_head` action instead.
@jeherve
Copy link
Member

jeherve commented Aug 5, 2019

would an improvement for now be to get rid of the workaround / fallback add_action( 'wp_head', 'stats_add_shutdown_action' ), drop the use of the $rendered_stats_footer global variable but just keep it adding the stats JS via the existingwp_footer action?

Yes, that'd be nice, all the while making sure we don't break compatibility with AMP as you mentioned. 👍

@rickcurran
Copy link
Contributor

Thanks, I've added a pull request #13182 for this now.

@jeherve jeherve removed this from the Not Currently Planned milestone Aug 5, 2019
mdawaffe pushed a commit that referenced this issue Aug 16, 2019
…rkaround for themes without wp_footer (#13182)

* Removed fallback / workaround for themes without wp_footer

See #3763
@mdawaffe
Copy link
Member

#13182 is a nice code simplification, but let's leave this issue open for now since we may still need to address the SRI issue.

@stale
Copy link

stale bot commented Feb 12, 2020

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

@jeherve
Copy link
Member

jeherve commented Sep 7, 2023

This was eventually implemented in #29780.

@jeherve jeherve closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. Hacktoberfest Support open source and earn a limited edition T-shirt! https://hacktoberfest.digitalocean.com/ [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

No branches or pull requests

6 participants