Skip to content

fix: put the pixel's data attributes on its own script tag - #3

Merged
onamfc merged 1 commit into
mainfrom
fix/pixel-tag-attributes
Oct 22, 2026
Merged

onamfc merged 1 commit into
mainfrom
fix/pixel-tag-attributes

Conversation

@onamfc

@onamfc onamfc commented Oct 22, 2026

Copy link
Copy Markdown
Member

Problem

On 0.2.0 the pixel sends no events at all. The page source looks right at a glance, but the attributes are on the wrong tag:

<script data-workspace="ws_…" data-visitors="true" id="linkforty-pixel-js-before">
window.LinkForty=window.LinkForty||{q:[]};
</script>
<script async data-wp-strategy="async" id="linkforty-pixel-js" src="https://cdn.linkforty.com/pixel.js"></script>

script_loader_tag receives the whole tag string for the handle, and that includes the inline before script added by wp_add_inline_script() (WP_Scripts::do_item() builds $translations . $before_script . <script src>). add_attributes() replaced the first <script, which is the bootstrap. The pixel reads its config from document.currentScript (its own src tag), finds no data-workspace, and stays silent — no console error, and window.LinkForty is still defined.

Fix

LinkForty_Pixel::add_attributes() now targets the tag with id="linkforty-pixel-js", matching either quote style (WordPress 6.3 prints id='…' via printf; 6.4+ uses wp_get_script_tag() with double quotes). If an optimization plugin has rewritten the id, it falls back to the first tag with a src. If neither matches, the tag is returned unchanged.

wp_script_attributes would be cleaner but is not applied to enqueued scripts on 6.3, which the plugin still supports.

Verification

  • WordPress 7.1.3 (Playground): plugin activated with a site key and visitor ID on; wp_print_head_scripts() output:
    <script id="linkforty-pixel-js-before">…</script>
    <script data-workspace="ws_…" data-visitors="true" async data-wp-strategy="async" id="linkforty-pixel-js" src="https://cdn.linkforty.com/pixel.js"></script>
  • WordPress 6.3 markup (Playground can't install 6.3, so add_attributes() was run against the exact string 6.3 prints): attributes land on the src tag. Also checked: a tag with no id (fallback to src), and another handle (untouched).
  • composer lint and composer phpcs pass.

Sites already on 0.2.0 are also covered from the pixel side: the pixel now falls back to any script[data-workspace] when its own tag has none. This release removes the dependence on that fallback.

The script_loader_tag filter receives the inline bootstrap printed before the
pixel as well as the pixel tag, and add_attributes() stamped the first
<script it found. The site key and every other data-* attribute landed on
the inline bootstrap, the pixel read its own tag through
document.currentScript, found no site key, and sent nothing.

Target the tag whose id is linkforty-pixel-js (single-quoted on WordPress
6.3, double-quoted from 6.4), falling back to the first tag with a src when
an optimization plugin has rewritten the id.
@onamfc
onamfc merged commit d42673e into main Oct 22, 2026
3 checks passed
github-actions Bot pushed a commit that referenced this pull request Oct 22, 2026
## <small>0.2.1 (2026-10-22)</small>

* Merge pull request #3 from LinkForty/fix/pixel-tag-attributes ([d42673e](d42673e)), closes [#3](#3)
* fix: put the pixel's data attributes on its own script tag ([4a17340](4a17340))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.2.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant