简体   繁体   English

URL 字符串中包含“utm_”会破坏 Wordpress 中的 $_GET 变量

[英]Having “utm_” in the URL string breaks the $_GET variable in Wordpress

First note: This site is hosted on WPEngine (varnish caching), but I can't seem to replicate the issue on another server.第一个注意事项:该站点托管在 WPEngine(清漆缓存)上,但我似乎无法在另一台服务器上复制该问题。

We need to be able to access the $_GET php variable on some pages.我们需要能够访问某些页面上的 $_GET php 变量。 For testing, I modified our Wordpress header.php to do a var_dump on the very first line.为了测试,我修改了我们的 Wordpress header.php 在第一行做一个 var_dump。

Normally, everything works OK.通常,一切正常。 However if the URL string contains "utm_", every subsequent variable in $_GET is stripped.但是,如果 URL 字符串包含“utm_”,则 $_GET 中的每个后续变量都会被剥离。 The extra weird part is that if I am logged in to Wordpress, everything works fine.更奇怪的是,如果我登录到 Wordpress,一切正常。

Our Paypal return URL looks like this:我们的 Paypal 返回 URL 如下所示:

http://oururl.com/buy/thankyou/?utm_nooverride=1tx=xxxxyyyy ... http://oururl.com/buy/thankyou/?utm_nooverride=1tx=xxxxyyyy ...

The utm_nooverride causes the $_GET to be an empty array. utm_nooverride 导致 $_GET 为空数组。 If I change it to "test=1&tx=xxxxyyyy", it works OK.如果我将其更改为“test=1&tx=xxxxyyyy”,则它可以正常工作。 If I use "utm_test=1&tx=xxxxyyyy" I get an empty array again.如果我使用“utm_test=1&tx=xxxxyyyy”,我会再次得到一个空数组。

There is nothing odd in the .htaccess, only a few standard Wordpress lines. .htaccess 中没有什么奇怪的,只有几行标准的 Wordpress 行。

Could there be something in the hosting causing this?托管中是否有某些原因导致了这种情况?

In case anyone else runs into the same problem, as I just did, I spoke with WPEngine support team via Live chat and.如果其他人遇到同样的问题,就像我刚刚做的那样,我通过实时聊天和 WPEngine 支持团队进行了交谈。 They rectified it within a few minutes他们在几分钟内纠正了它

Here's a shortened transcript of our chat:这是我们聊天的简短记录:

  • Me: I'm trying to put some GET vars into a cookie and it seems like it works for arbitrary variables (like my_name=bob) when accessing the $_SERVER['REQUEST_URI'] global variable, but for some reason anything starting with "utm_" in the query string is being stripped out.我:我正在尝试将一些 GET 变量放入 cookie 中,当访问 $_SERVER['REQUEST_URI'] 全局变量时,它似乎适用于任意变量(如 my_name=bob),但出于某种原因,任何以“ utm_" 在查询字符串中被删除。 Seems to be a php/cache config on your side, do you know anything about this?好像是你这边的 php/cache 配置,你知道吗?
  • WPE: Great question; WPE:好问题; unfortunately, I am not aware of anything automatically stripping out specific args within a query.不幸的是,我不知道有什么会自动去除查询中的特定参数。 Let me review this with some colleagues.让我和一些同事一起回顾一下。
  • Me: k.我:k。 FYI, here's a stack overflow issue http://stackoverflow …-the-get-variable-in-wordpress.仅供参考,这里有一个堆栈溢出问题http://stackoverflow ...-the-get-variable-in-wordpress。 seems to be corroborated by others' experience as well: https://twitter.com/ …ey01/status/555584796785528832似乎也得到了其他人的经验的证实: https ://twitter.com/…ey01/status/555584796785528832
  • WPE: This is for the your install: ? WPE:这是为了您的安装:?
  • Me: yes我可以
  • WPE: Can I please have you try now? WPE:我可以请你现在试试吗?
  • Me: ok, it works now.我:好的,现在可以了。 what was the issue?问题是什么?
  • WPE: Excellent! WPE:太棒了! The issue was that we strip out the "utm_" arg by default.问题是我们默认去掉了“utm_”参数。 I apologize for not realizing the arg that you were suggesting.我很抱歉没有意识到您建议的 arg。 I had to exclude this arg from our caching system.我不得不从我们的缓存系统中排除这个参数。
  • Me: ok, so there's no way I could have done that myself right?我:好的,所以我自己不可能做到这一点,对吗?
  • WPE: That is correct. WPE:没错。

Link for reference: https://wpengine.com/support/utm-gclid-variables-caching/参考链接: https : //wpengine.com/support/utm-gclid-variables-caching/

WP Engine may have (mis)configured Varnish to ignore query string parameters when they reference Google Analytics campaign variables. WP 引擎可能(错误)配置了 Varnish,以在它们引用 Google Analytics 活动变量时忽略查询字符串参数。 They may have done this so they can reference the cache of the page without the query string, since the campaign variables are read client-side (not server-side) by analytics provider.他们可能已经这样做了,以便他们可以在没有查询字符串的情况下引用页面的缓存,因为活动变量是由分析提供商在客户端(而不是服务器端)读取的。 Therefore ignoring these variables server-side would ostensibly have no effect, and it would improve performance for sites making heavy use of inbound Google Analytics tracking.因此,在服务器端忽略这些变量表面上没有任何影响,并且会提高大量使用入站 Google Analytics 跟踪的网站的性能。

I say it's possible since there's a Stack Overflow question asking how to do just that: "Stripping out select querystring attribute/value pairs so varnish will not vary cache by them" .我说这是可能的,因为有一个 Stack Overflow 问题询问如何做到这一点: “剥离选择的查询字符串属性/值对,以便清漆不会因它们而改变缓存” The only way to know for certain is to contact WP Engine.确定的唯一方法是联系 WP Engine。

I'm currently chatting with WPEngine in the hopes of getting this issue resolved.我目前正在与 WPEngine 聊天,希望能解决这个问题。

WPEngine's varnish cache does in fact strip out utm_ and gclid_ parameters in an effort to improve caching. WPEngine 的 varnish 缓存实际上去除了 utm_ 和 gclid_ 参数以改进缓存。 Sadly, WPEngines implementation of this "feature" strips out all subsequent query parameters after the first utm_ or gclid_ parameter is identified.可悲的是,在识别第一个 utm_ 或 gclid_ 参数后,WPEngines 实现此“功能”会删除所有后续查询参数。

For example the the URL: www.example.com/test/?foo=bar&utm_source=email&page=1例如 URL: www.example.com/test/?foo=bar&utm_source=email&page=1

What you would expect your server to receive: www.example.com/test/?foo=bar&page=1您希望您的服务器收到什么: www.example.com/test/?foo=bar&page=1

What your server actually receives: www.example.com/test/?foo=bar您的服务器实际收到的内容: www.example.com/test/?foo=bar

Notice how the page=1 parameter is removed even though it is not a utm_ or gclid_ parameter.请注意 page=1 参数是如何删除的,即使它不是 utm_ 或 gclid_ 参数。

WPEngine's proposed workaround is to apply utm_ and gclid_ to the cache exclusion list, but this means that cache will not be served if ever there is aa utm_ or gclid_ parameter in your url. WPEngine 建议的解决方法是将 utm_ 和 gclid_ 应用于缓存排除列表,但这意味着如果您的 url 中有 utm_ 或 gclid_ 参数,则不会提供缓存。 This seems less than ideal as if a URL has a utm_ or gclid_ parameter it is most likely from an email, and doing large email send outs means a spike in traffic which is exactly when you would want to serve a cached page.这似乎不太理想,因为 URL 有一个 utm_ 或 gclid_ 参数,它很可能来自电子邮件,并且发送大量电子邮件意味着流量激增,而这正是您想要提供缓存页面的时候。

Below is some javascript that detects if the utm_ or gclid_ is in the URL and if it is it rearranges the url so that the utm_ and gclid_ parameters are at the end of the query string and then triggers a page redirect.下面是一些 javascript,它检测 utm_ 或 gclid_ 是否在 URL 中,如果是,它会重新排列 url,以便 utm_ 和 gclid_ 参数位于查询字符串的末尾,然后触发页面重定向。 The code below specifically looks for a parameter called tfa_next which is a parameter added on to the end of a URL via a FormAssembly redirect.下面的代码专门查找名为 tfa_next 的参数,该参数是通过 FormAssembly 重定向添加到 URL 末尾的参数。 The below code could be improved to be more generic but I hope it serves as a starting point for anyone who needs it.下面的代码可以改进为更通用,但我希望它可以作为任何需要它的人的起点。

const params = new URLSearchParams(window.location.search);
var newParams = "";
console.log("This is working");
if(params.has('tfa_next')){
  console.log("it has a tfa_next param");
  if(window.location.href.includes("UTM_") || window.location.href.includes("utm_") || window.location.href.includes("GCLID_") || window.location.href.includes("gclid_")){
    console.log("it has a utm param");
    var count = 0;
    for(const [key, value] of params) {
      if(count == 0 && key == "tfa_next"){
        break;
      } else {
        count += 1;
      }
      if(key.includes("UTM_") || key.includes("utm_") || key.includes("GCLID_") || key.includes("gclid_")){
        newParams = newParams + key + "=" + encodeURIComponent(value) + "&";
      } else {
         newParams = key + "=" + encodeURIComponent(value) + "&" + newParams;
      }
      console.log("NewParams: " + newParams);
    }
    if(newParams.slice(newParams.length - 1) == "&"){
      newParams = newParams.slice(0, newParams.length - 1);
      console.log("final query string: " + newParams);
    }
    if (count > 0) {
        console.log("end result: " + window.location.protocol + "//" + window.location.hostname + window.location.pathname + "?" + newParams);
     window.location.replace(window.location.protocol + "//" + window.location.hostname + window.location.pathname + "?" + newParams);
    }

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM