Skip to content

feat: add internal traffic IP config to tag requests with TWN-Source header#395

Open
ahuininga-orisha wants to merge 7 commits into
developfrom
feat/230536-internal-traffic-ip-config
Open

feat: add internal traffic IP config to tag requests with TWN-Source header#395
ahuininga-orisha wants to merge 7 commits into
developfrom
feat/230536-internal-traffic-ip-config

Conversation

@ahuininga-orisha

Copy link
Copy Markdown
Collaborator

Customers and merchants often want to filter out internal traffic — such as developers, testers, or office employees — from Tweakwise Analytics reports. Because all Tweakwise API requests are server-to-server, Tweakwise only sees the Magento server's IP address, making standard IP-based filtering impossible on their end. Tweakwise provides a TWN-Source header mechanism specifically for this purpose, but it previously required custom implementation by the merchant's development team.

This change adds native support for internal traffic tagging directly in the plugin. A merchant can now enter a comma-separated list of IP addresses in the Magento admin, and any Tweakwise API request triggered by a visitor matching one of those IPs will automatically include the TWN-Source: Internal-Traffic header. This covers all request types: navigation, search, autocomplete, recommendations, and analytics events (which are proxied through the Magento controller, not sent directly from the browser).

Changes

File What changed
etc/adminhtml/system.xml New textarea field internal_ip_addresses added to the General group (sortOrder 120)
Model/Config.php New getInternalIpAddresses() method reads and parses the comma-separated config value
Model/Client.php Injected RemoteAddress; added buildInternalTrafficHeader() helper; applied to both createGetRequest() and createPostRequest()
README.md Documented the new setting as item 3 under the General section

How to test

Scenario 1 — Config field visible in admin

  1. Go to Stores → Configuration → Catalog → Tweakwise → General.
  2. Verify a new Internal traffic IP addresses field is present below the Language field.
  3. Confirm the comment text describes comma-separated IPs and the TWN-Source header behaviour.

Scenario 2 — Header sent for matching IP

  1. Find your local browser IP (for a local dev setup this is typically 127.0.0.1).
  2. Enter that IP in the Internal traffic IP addresses field and save.
  3. Run bin/magento cache:clean config.
  4. Enable HTTP request logging or use a proxy (e.g. Charles, mitmproxy) to intercept outgoing requests from Magento to gateway.tweakwisenavigator.net.
  5. Browse to a category page (e.g. /default/women/tops-women2/).
  6. Verify the outgoing Tweakwise navigation request contains the header TWN-Source: Internal-Traffic.

Scenario 3 — Header not sent for non-matching IP

  1. Set a non-matching IP (e.g. 10.0.0.99) in the config and save.
  2. Run bin/magento cache:clean config.
  3. Browse to the same category page.
  4. Verify the outgoing Tweakwise request does not contain a TWN-Source header.

Scenario 4 — Header not sent when config is empty

  1. Clear the Internal traffic IP addresses field and save.
  2. Run bin/magento cache:clean config.
  3. Browse to the category page.
  4. Verify no TWN-Source header is present on the Tweakwise request.

Scenario 5 — Multiple IPs and whitespace tolerance

  1. Enter 127.0.0.1 , 10.0.0.1 , 192.168.1.1 (with extra spaces) in the config and save.
  2. Run bin/magento cache:clean config.
  3. Browse from an IP matching one of the listed addresses.
  4. Verify TWN-Source: Internal-Traffic is sent.
  5. Verify no PHP errors or warnings appear in var/log/exception.log.

Scenario 6 — Analytics events also tagged

  1. Set your IP in config and save; clear config cache.
  2. Enable analytics in Tweakwise → General → Send analytics events.
  3. Browse to a category page and click a product.
  4. Intercept the POST request to /tweakwise/ajax/analytics and the subsequent server-side call to navigator-analytics.tweakwise.com.
  5. Verify the outgoing analytics POST to Tweakwise includes TWN-Source: Internal-Traffic.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant