=== Tragly — Server-Side Tagging & GA4 Ecommerce ===
Contributors: unit45host
Tags: google tag manager, analytics, ga4, woocommerce, server-side tagging
Requires at least: 6.4
Tested up to: 7.0
Requires PHP: 8.1
Stable tag: 0.10.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Connect WordPress to Tragly: a first-party container loaded from your own domain, GA4-shaped WooCommerce events, campaign attribution and Google reports in wp-admin.

== Description ==

Tragly runs your Google Tag Manager container on its own servers and serves the loader from a hostname on **your** domain. Browser tracking prevention and ad blockers cut off requests to `googletagmanager.com`; a first-party host is not on those lists, so the measurement you already paid for actually arrives.

This plugin is the WordPress half of that. At its core it does three things:

1. **Connects** your site to your Tragly account with an API key.
2. **Writes the container** into `wp_head` and `wp_body_open`, wrapped in the comment markers Tragly's own scanner looks for.
3. **Pushes WooCommerce ecommerce events** into the `dataLayer` in the exact shape GA4 expects — `ecommerce` object, `items` array, numbers as numbers.

Around that core:

* **Self-serve setup.** Tragly looks at your site for an existing GA4 property or GTM container and asks whether to reuse your measurement ID or create a new one. It proves the site is yours through a one-minute challenge on `/wp-json/tragly/v1/verify`, then builds the containers. If Site Kit already writes a Google tag, you can switch that off (and back on) from the same screen, so nothing is counted twice.
* **Campaign attribution.** UTM parameters and ad click IDs (`gclid`, `fbclid`, `msclkid`, …) are kept as first and last touch in a first-party cookie, filled into hidden form fields, and added to leads and purchases. The cookie is only written after consent.
* **Reports.** GA4 traffic, Search Console queries and a PageSpeed measurement on a **Tragly → Reports** screen and a dashboard widget, plus an optional weekly e-mail. Tragly fetches the numbers; the plugin still never calls a Google API itself.

= What it does not do =

These are deliberate, and they are the reasons the plugin is safe to leave installed:

* **It never calls a Google API.** Google Tag Manager's API quota is counted per Google *user*, not per site — a handful of plugins polling it would exhaust the account for everyone on it. Only Tragly's servers talk to Google.
* **It never sends your API key to the browser.** The key is stored with autoloading disabled, redacted out of every log line, and shown in the admin only as a prefix and its last four characters.
* **It never puts personal data in the dataLayer.** No email address, phone number, or name — not hashed, not plain. Only product, order and page facts.
* **It never overrides your consent platform.** If one is active, this plugin will not write a second Consent Mode default block over a visitor's stored choice — that is not configurable. If none is active, it writes a default-denied block, because the snippet you would otherwise paste by hand carries one too; you can switch that off.
* **It never fakes a connection.** With no key, or with container details missing, it writes nothing at all and says exactly which field is missing. It has no fallback container and no default account.

= WooCommerce events =

`view_item_list`, `view_item`, `select_item`, `add_to_cart`, `remove_from_cart`, `view_cart`, `begin_checkout`, `add_shipping_info`, `add_payment_info`, `purchase`.

Page-level events are pushed server-side, before `wp_head`, so a theme script cannot suppress them. Only the genuinely browser-side interactions — a click in a product list, an AJAX add-to-cart, choosing a shipping method — run in JavaScript.

Prices are the prices your customer actually saw: tax handling follows your WooCommerce display settings, and order items report what was paid, per unit, after line discounts. Values are decimal numbers with a point, never a formatted string — a `value` of `"1 299,00 Ft"` is `undefined` revenue in GA4, and that is the single commonest way ecommerce reporting silently breaks.

`purchase` fires once per order. It is marked as sent on the order itself before the push, so a customer refreshing the thank-you page cannot duplicate the revenue.

= Leads, thank-you pages and contact clicks =

Successful submissions of WPForms, Gravity Forms, Ninja Forms, Contact Form 7 and Elementor Pro Forms push `generate_lead` with the form's ID and name. Clicks on phone and email links push `phone_click` and `email_click`. No field value and no phone number or address is ever pushed.

Under **Settings → Thank-you pages** you can list pages that push an event when a visitor arrives, one `path event_name` per line — for example `/thank-you/ generate_lead` or `/newsletter-thanks/ sign_up`. The list is empty until you add one. How it counts:

* **Once per browser session per page.** Reloading the page or coming back to it with the back button does not count again.
* **Elementor Pro forms that redirect** to a listed page are counted on arrival instead of at submission, with the same `event_id`, the form's ID and `form_source: elementor`. A redirect leaves the page before tags have reliably sent anything; arriving does not.
* **Any other visit counts too**, with a new `event_id` and `form_source: thank_you_page`. That is what measures forms from other plugins that redirect to the page — and it also means someone who opens the page directly is counted. Keep thank-you pages out of menus and sitemaps.
* If a form plugin other than Elementor Pro already pushes `generate_lead` at submission *and* redirects to a listed page, the lead is counted twice. List the page only for forms that do not report themselves.
* Only the path and the event name are pushed (`page_type: thank_you`, `page_path`), never the query string, where form plugins often put what was typed.

= Requirements =

* A Tragly account and an API key with the `read`, `sites.write` and `ingest` scopes. Connecting from the Connection screen asks for all three; a pasted key without `ingest` works for everything except server-side delivery (refunds), and the Status screen says so.
* WordPress 6.4+, PHP 8.1+.
* WooCommerce is optional; without it the ecommerce layer simply does not load.
* Your theme should call `wp_body_open()`. If it does not, the plugin tells you so on its own screens — the `noscript` half of the container has nowhere to go, and visitors without JavaScript go unmeasured.

= Languages =

The plugin ships in English and Hungarian (`hu_HU`). WordPress picks the language from **Settings → General → Site Language**, or from your own profile language on the admin screens. Event names, scope names and the snippet's comment markers are identifiers and stay as they are in every language.

= Other products from the same team =

Tragly is one of several products built by the same people. The plugin lists them on a **Tragly → Ecosystem** screen and in a one-line row at the bottom of its own settings pages — nowhere else. There are no dashboard notices, no banners on other plugins' pages, and nothing on your site's front end.

Ad Campaign Crafter (adcampaigncrafter.com) · AdsPilot (adspilot.im) · WEB Audit (webaudit.me) · ADS Guardian (adsguardian.me) · ShipKit (shipkit.page) · 360 Marketing (360marketing.cc) · FWMOS (fwmos.com) · ConsentKit (consentkit.cc) · MSPS.one (msps.one)

These are our own products, not paid placements. The plugin behaves identically whether you use all of them or none, and the list is part of the download — displaying it does not contact any server.

== Installation ==

1. Install and activate the plugin.
2. Go to **Tragly → Connection**.
3. Press **Connect to Tragly**. You are taken to tragly.com to sign in and approve this site; the key comes back on its own.
4. The plugin registers this site's domain with Tragly and mirrors the container details back.
5. Check **Tragly → Dashboard**. When the Measurement card says the container is active, the snippet is on your pages.

You never copy or paste the key, and it does not travel through the address bar. This site keeps a secret only it holds and sends out nothing but a hash of it, so the code that comes back through your browser is worthless on its own. If you already have a key, or this site is not reachable from the internet, you can still paste one by hand on the same screen.

== Frequently Asked Questions ==

= I already have Google Tag Manager on this site. =

Then decide which container stays. Two containers on one page is a race, not a setting: consent state is shared across the page and load order decides which one wins. The plugin will detect the other container — by actually fetching your front page and reading it, not by guessing from a plugin list — and warn you. It will not remove anything of yours.

= Where is my API key stored? =

In `wp_options`, with autoloading off so it is not read on every front-end request. It is never printed to the page, never sent to JavaScript, and it is stripped out of debug log lines before they are written. Tragly itself stores only a hash of it, which is why nobody can show you the key a second time.

= Does "Disconnect" revoke my key? =

It offers to, and the box is ticked by default. Untick it only if the same key is deliberately used somewhere else, which would lose access the moment it is revoked. Deleting the plugin always revokes: a key left alive after the plugin is gone can still read your account, and nothing would remind you it exists.

= Do I need a cookie banner? =

That depends on your site and your jurisdiction, and this plugin will not answer it for you. What it does: if a consent platform is active, it leaves consent entirely to that platform. If none is active, it sets every consent category to "denied" before the container loads — which, with nothing on the site able to grant them, means no measurement until you add a banner. That is deliberate, it is stated on the Settings screen, and you can switch it off there.

= Nothing is being measured. Why? =

**Tragly → Status** names the reason rather than showing a generic error: no key, key rejected, container still provisioning, or a specific container field Tragly has not sent yet. The plugin refuses to write a half-correct snippet, because one that loads but is invisible to Tragly looks like it works and measures nothing.

= How do I check that my installation is verified? =

Open **Tragly → Status** and press **Refresh from Tragly**. The **Installation proof** section reads **Verified** once Tragly has signed this site's installation and the signed set is current. **Tools → Site Health** goes one step further: it reports "The Tragly installation is verified" only when both blocks carry the proof and the last front-page check found them in place; otherwise it names what is wrong. A site Tragly has not signed yet shows a legacy installation: the snippet works as before, it just cannot be proven.

= Does the plugin tell Tragly whether my installation is verified? =

Yes, once the site is connected. When the verdict Site Health shows changes, and otherwise once a day, the plugin sends Tragly the state (verified, degraded, legacy, or disabled when the snippet is switched off in Settings), the first reason if it is degraded, the plugin version, and the snippet version and deployment of the signed set. It sends nothing about visitors. A report that fails is tried again later and changes nothing on the page.

= What does "The verified Tragly installation is degraded" mean? =

Once Tragly has signed this site's installation, the snippet carries a proof that it is the installation Tragly issued. When the proof no longer holds (the signed set expired, the container changed since it was signed, or the last front-page check found a block missing or out of place), the plugin keeps measuring and writes the block without the proof. Tragly's screens and **Tools → Site Health** say so and name the reason.

A site that would rather not measure than measure unproven can add `define( 'TRAGLY_HIGH_ASSURANCE', true );` to `wp-config.php`. A verified site then does not write a block that cannot carry its proof, and Site Health reports a critical issue. It is off by default, and it changes nothing on a site without a signed installation.

= How does the plugin update? =

From Tragly, through the normal WordPress update screens. The plugin is not on WordPress.org, so its header names tragly.com as the update source and WordPress does not ask WordPress.org about it. About twice a day the plugin reads https://tragly.com/.well-known/tragly-plugin-release.json, a release record Tragly signs with the same pinned key as the installation proof. An update is offered only when the signature checks out, the record has not expired, and the version is newer than the installed one: never a downgrade. When the update runs, the downloaded package must match the SHA-256 in the signed record, or it is deleted and nothing changes.

**What an update tells Tragly (since 0.9.0).** When you press Update on a connected site, the plugin asks Tragly once more, at that moment, for a short-lived download link. That request carries your API key in the request header and exactly two facts in its body: this installation's ID (the `app_id` of the verified installation) and the installed plugin version. Tragly records which key asked, the site it belongs to, that installation ID, the old and new version and the package checksum, so an update can be accounted for. It does not receive your domain, an e-mail address or anything about your visitors in this request. The link only changes where the same signed package is fetched from: the record must be identical to the one already verified, the package must still match the signed SHA-256, and if the request fails for any reason, or the site is not connected, the plugin updates from the public package link exactly as before. Revoking the key stops this accounting, not the update. Whether updates install by themselves is the usual WordPress auto-update setting, off unless you turn it on.

== Current limitations ==

Stated plainly, because finding these out after installing is worse:

* **Refund delivery needs the submission token.** A refund happens in wp-admin, where no container runs, so there is no browser to push the event into — it travels server-side instead, which requires the submission token from the Status screen. Without one, refunds are captured and counted there rather than dropped, and are delivered as soon as the token exists. The token can be created once from the Status screen; replacing it is done by Tragly, not from wp-admin.
* **The verified snippet has been confirmed on two live sites so far**, with the version 1 proof (since 0.8.0). Version 2 proofs (0.8.6), the stricter placement check (0.8.7) and the degraded-installation warning (0.8.8) have been tested against signed test sets, not yet on a live site. Purchase and form events, and their de-duplication with Tragly's server-side events under each consent state, have not yet been measured end to end on a live store.
* **Signed updates are new in 0.8.11, and the keyed update request in 0.9.0.** Both have been tested against records signed by Tragly's reference code and against Tragly's documented responses; a live update through the keyed request on a real site has not been measured yet.
* **Self-serve setup and reports are new in 0.7.0** and have been tested against Tragly's documented responses, not yet end to end on a live site. Setup needs your home URL to be reachable over HTTPS, and a security plugin that blocks the REST API will stop the ownership check — the error message names this.

== Screenshots ==

1. The Dashboard: connection, measurement, events, consent and plan at a glance.
2. The Connection screen: one-click connect, connection state, and the domain this site registers as.
3. The Status screen: container fields, usage, and what else is measuring on the site.
4. The Settings screen: which events to push, and who owns consent.
5. The Ecosystem screen: the other products from the same team.

== Changelog ==

= 0.10.0 =
* Groundwork for a ConsentKit banner written by the plugin: the code that checks and prints it is included, but nothing turns it on yet. A site updating from 0.9.0 sees no change.

= 0.9.0 =
* New: when you update a connected site, the plugin asks Tragly for a short-lived download link, sending only the installation ID and the installed version (the key travels in the header). The signed record and the SHA-256 check are unchanged, and any failure falls back to the public package. Details under "How does the plugin update?".

= 0.8.14 =
* Maintenance release: a code comment that named internal infrastructure was reworded. No change in behaviour.

= 0.8.13 =
* A ConsentKit plugin is now recognised by its own signal as well as by its folder name, so the plugin never writes a second consent default next to it.

= 0.8.12 =
* A release record is now rejected with the same error, in the same order, that Tragly itself reports.

= 0.8.11 =
* New: updates from Tragly through the normal WordPress update screens. A release is offered only when Tragly has signed it and it is newer than the installed version, and the downloaded package must match the signed checksum before WordPress installs it. Nothing installs by itself unless auto-updates are turned on.

= 0.8.10 =
* New: the plugin reports the installation's state to Tragly when it changes and once a day, so a site that loses its proof is seen on Tragly's side too. The report carries no visitor data; a failed report changes nothing on the page.

= 0.8.9 =
* New: Hungarian translation (`hu_HU`).
* Fixed: the translation template reported bugs against the wrong plugin slug.
* Docs: how to check a verified installation, and what has been confirmed on live sites.

= 0.8.8 =
* New: a verified installation whose proof no longer holds is named on Tragly's screens and in Site Health, with the reason. Measurement goes on without the proof.
* New: optional high-assurance mode, off by default. With `define( 'TRAGLY_HIGH_ASSURANCE', true );` a verified site withholds a block it cannot write with its proof.

= 0.8.7 =
* Changed: once a signed installation proof is in place, a front page without the head or the body block is an error on the Status screen. The proof covers both blocks, so such a page does not carry the verified installation. A body block that is not the first thing after the body tag shows the installation as degraded. Without a proof both stay plain reports, as before. The printed snippet is unchanged.

= 0.8.6 =
* New: installation proofs of snippet version 2 are accepted. In version 2 the conversion bridge passes only the event name and its id to the dataLayer; version 1 also passed any extra key. Sets signed as version 1 keep verifying. A set of any other version is refused as a whole (`unknown_snippet_version`), and the page keeps its last good proof or the plain snippet. The printed snippet is unchanged.

= 0.8.5 =
* New: when this server's PHP cannot check Ed25519 signatures, Tragly's screens say so in an error notice that names the fix (the PHP sodium extension). Measurement is not affected.
* New: a certificate or TLS failure has its own status. Calls to Tragly report `tragly_tls_error`, and the front-page check reports `tragly_scan_tls_failed` and names the staging switch `TRAGLY_SCAN_SKIP_TLS_VERIFY`. Timeouts and unknown hosts stay plain transport errors.

= 0.8.4 =
* New: a signed installation manifest is valid for seven days from its issue time, then for one more day of grace. In the grace day the page keeps the proof and the Status screen warns; after that the page prints the unverified snippet, which loads the same container. The Status screen shows the valid-until date.
* New: the plugin asks Tragly for a current manifest once a day (WP-Cron), only on a connected site whose container is ready. A failed request is recorded on the Status screen, never thrown.
* Hardening: a manifest that is already expired, or dated more than five minutes ahead of this server's clock, is refused and the last good set stays. Only the contract's exact date form is read.
* Tests: the keys of every dataLayer push are pinned (forms, thank-you pages, phone and email clicks); no field value, query string or contact detail is pushed.

= 0.8.3 =
* New: Tragly's standby signing key (tpk_2027a) is pinned next to the production key, so a key rotation on Tragly's side keeps installations verified without waiting for a plugin update. Tragly does not sign with it yet; both keys are published at https://tragly.com/.well-known/tagproof-keys.json.

= 0.8.2 =
* Hardening: an installation set is accepted only if all its manifests come from one issue — same site, containers, deployment, time and signing key. A set that mixes two is refused as `mixed_set` and the last good set stays.
* Hardening: a PHP without Ed25519 support reports `crypto_unavailable` instead of `bad_signature`, and any error from the verifier counts as a failed check, never a fatal error.
* Fix: GTM container ids of 11 and 12 characters are accepted, the same bound Tragly's server uses. A trailing newline is refused.
* Hardening: the front-page conflict scan checks the TLS certificate. A staging site on a self-signed certificate can turn this off with `define( 'TRAGLY_SCAN_SKIP_TLS_VERIFY', true );` in `wp-config.php`.
* Docs: Requirements name the `ingest` scope; Current limitations say what has and has not been confirmed on live sites.

= 0.8.1 =
* New: a Support info block at the foot of the Status screen names the plugin version and the build it was packaged from, the Tragly server build, the key that signed this site's installation, and the WordPress and PHP versions. It is shown in wp-admin only, never on the public site.

= 0.8.0 =
* New: installation proof. When Tragly has issued a signed installation manifest for this site, the plugin fetches it, checks its Ed25519 signature against a key pinned in the plugin, and prints the snippet inside a `tagproof:v1` block within the existing markers. Anything that does not check out prints the snippet exactly as before. The private key never reaches WordPress.
* New: the Status screen shows whether the installation is verified, why a manifest was refused, why the last request for it failed, and where the snippet landed on the front page.
* New: when Tragly prepares a replacement event submission token, the next refresh takes it and stores it; the old token keeps working until then, and if taking it fails. The Status screen says when it happened.
* New: Tragly's production signing key (tpk_2026a, also published at https://tragly.com/.well-known/tagproof-keys.json) is pinned in the plugin, so a site whose installation Tragly has signed shows the verified snippet. Sites without a signed installation keep the legacy snippet, unchanged.

= 0.7.2 =
* New: when Tragly's own container loads the consent banner (ConsentKit), Settings and the dashboard name it as "ConsentKit (from the Tragly container)" instead of reporting that no consent platform was found, and ConsentKit is no longer suggested to that site. The plugin's own consent default and the attribution cookie's consent wait work as before.

= 0.7.1 =
* New: Elementor Pro Forms push `generate_lead` (form ID, form name, `form_source: elementor`) when a submission succeeds. No field value is pushed, not even hashed.
* New: on by default, the plugin pushes `phone_click` or `email_click` when a visitor clicks a phone or email link (switch it off under Settings). Only the fact of the click is pushed, not the number or the address.
* New: browser-side `generate_lead`, `phone_click` and `email_click` carry a random `event_id`, so a Pixel and a server-side copy of the same event can be deduplicated.
* Changed: form submissions are tracked as `generate_lead` by default. A site that already saved the switch off keeps it off.
* Changed: the form script now loads on pages where Elementor Pro is the only form plugin.
* Changed: the check for other tag managers runs on its own in the background — daily, and shortly after connecting or saving settings. The Status screen only shows the result, and says that it covers the front page and the plugin list.
* New: thank-you pages — list paths under Settings, each with the event it pushes on arrival (once per session). Elementor Pro forms that redirect there are counted on arrival with the same event ID.
* Removed: rotating the submission token from wp-admin. The Status screen shows only whether a token is stored; replacing it is done by Tragly.
* Changed: the usage figure now says it is Tragly's count and that server-side tagging traffic is not included yet, so a working site can show 0.

= 0.7.0 =
* New: self-serve setup on the Status screen. Scan the site for an existing GA4 or GTM, choose to reuse your GA4 measurement ID or create a new one, and start; the progress updates on its own.
* New: a site-ownership check at `/wp-json/tragly/v1/verify`. It answers only while a setup is running and forgets the challenge as soon as the setup has started.
* New: Site Kit's Google tag can be switched off from the Status screen to avoid double counting, with a backup and a one-click restore. Deleting the plugin restores it as well.
* New: campaign attribution. UTM parameters and click IDs are stored as first and last touch in the `tragly_attr` cookie after consent (Consent Mode, Complianz, CookieYes, Cookiebot, Borlabs, WP Consent API), filled into hidden form fields (Tragly names or UTM Tracker names), pushed once per page as `tragly_attribution`, and attached to `generate_lead` and `purchase`.
* New: Tragly → Reports — GA4, Search Console and PageSpeed for the last 7 or 28 days, a "Measure now" button for PageSpeed, a dashboard widget, and a weekly e-mail switch. Responses are cached for about 15 minutes; when reports are not available on your plan or server, the screen says so instead of failing.
* A rate limit on setup or on PageSpeed no longer pauses every other call to Tragly.

= 0.6.3 =
* Fixed: connecting to Tragly failed with "Tragly approved the connection but did not return a usable key", and a pasted key was refused. Tragly now issues keys 43 characters long; the plugin accepted exactly 40. It now accepts 40 to 64.

= 0.6.2 =
* Fixed: the renewal date on the Dashboard's plan card. Tragly reports the period end in milliseconds and the card read it as seconds, so a monthly renewal was shown tens of thousands of years out.
* A plan that does not lapse now says "Does not expire" instead of a renewal date, and a plan without an event limit says so instead of showing a ten-digit quota.
* Plan names are shown capitalised ("Enterprise").
* The translation template now carries every string in the plugin; twelve added in 0.6.0 were missing from it.

= 0.6.1 =
* ConsentKit is recognised as a consent platform. When both plugins run on the same site, ConsentKit writes the Consent Mode default and Tragly stands down, so the default is never written twice.

= 0.6.0 =
* Refunds are now delivered. A refund issued in wp-admin travels server-side to Tragly, authenticated with the submission token from the Status screen. Anything captured before the token existed is sent the moment you create one, and a stuck queue has a "Deliver now" button that says why it is stuck.
* Delivery trusts the response body, not the HTTP status: Tragly answers a rejected submission with a 200, and a plugin that read the status alone would file lost events as delivered. Nothing is removed from the queue until Tragly's answer says the events were accepted.

= 0.5.0 =
* New Dashboard screen, and it is what the Tragly menu now opens: connection, measurement, events, consent and plan on five cards, with one sentence saying what to do if something is wrong.
* Suggestions on the Dashboard are worked out on your own site — from whether you have a consent banner, whether WooCommerce is running, whether an ad pixel is installed — and each one tells you which observation produced it. Nothing is sent anywhere to generate them.
* Consent Mode v2 default is now on for new installs. The snippet you would otherwise paste by hand carries the same block, so installing the plugin should not leave you with less. Sites that were already running keep their current setting and are told about the change once.
* Disconnect can now revoke the key at Tragly, and deleting the plugin always does. A key left behind after the plugin is gone can still read your account.

= 0.4.0 =
* One-click connect: the Connection screen now sends you to tragly.com to approve this site, and the key comes back on its own. You can still paste a key by hand if you prefer, or if this site is not reachable from the internet.
* The key never travels through the address bar. This site keeps a secret that only it holds, sends out nothing but its hash, and the approval code alone is worthless without it.
* Fixed: removing a product from the cart did not report remove_from_cart. WooCommerce drops the product object from the line before the event this plugin listened for, so the event was silently skipped.

= 0.3.0 =
* New screen: the other products from the same team, with a short description of each. It is a static list shipped with the plugin — nothing is fetched, and no data about your site leaves WordPress because of it.
* The plugin still makes exactly one kind of outgoing request, and it goes to tragly.com.

= 0.2.0 =
* The plugin now honours the Retry-After header when Tragly asks it to slow down, and waits instead of retrying into a limit that has not expired.
* Clearer guidance on the Connection screen: a direct link to the API keys page, and a note that disconnecting here does not revoke the key at Tragly.
* Clearer wording on the Settings screen about what the Consent Mode default does, and what happens when it is left switched off.

= 0.1.0 =
* First release. Connection, container snippet with Tragly markers, WooCommerce GA4 ecommerce dataLayer, conflict detection, consent ownership detection.
