URL 重写 Google 分析
URL rewrite for Google analytics
我搜索了很多S.O。问题和答案,但还没有看到一个很好的解决方案。我已经在我的 web.config 文件中创建并完善了 SEO URLs。我使用 rewriteMap 基本上将我的产品页面转换为我需要的任何 URL。
这是我必须执行的操作:
<rule name="Redirect Rule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RedirectMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Rewrite Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RewriteMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
现在要求的是添加 Google 分析参数以跟踪活动。如果我简单地添加参数,它当然会给我一个 404 错误。我现在正试图让这些 SEO URLs 获取这些参数,但感到非常沮丧。
如果我删除查询字符串,Google Analytics 将不会提取它,因为它是客户端脚本。类似这样的内容可能很接近,但我没有指向任何特定页面。
这就是我删除查询字符串的方式:
<rule name="Subject redirect with query" stopProcessing="true">
<match url="^(.*)" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
总结一下,这个怎么转:
https://www.example.com/fancy-url
至此
https://www.example.com/fancy-url?utm_source=Targeted_Email&utm_medium=email&utm_campaign=campaigntag
经过多次挫折,我终于弄明白了。查询字符串没问题,但其中一项设置阻止了其余规则的处理。在规则名称之后,我将停止处理设置为 true。当我在 IIS 中查看它时,该设置更具描述性。它说 "Stop processing of subsequent rules"。一旦我删除它并将其移到我的其他规则之上,就完美地工作了。
<rule name="Google Query string">
<match url="(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Rewrite" url="{R:0}" appendQueryString="false" />
</rule>
我搜索了很多S.O。问题和答案,但还没有看到一个很好的解决方案。我已经在我的 web.config 文件中创建并完善了 SEO URLs。我使用 rewriteMap 基本上将我的产品页面转换为我需要的任何 URL。
这是我必须执行的操作:
<rule name="Redirect Rule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RedirectMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Rewrite Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RewriteMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
现在要求的是添加 Google 分析参数以跟踪活动。如果我简单地添加参数,它当然会给我一个 404 错误。我现在正试图让这些 SEO URLs 获取这些参数,但感到非常沮丧。
如果我删除查询字符串,Google Analytics 将不会提取它,因为它是客户端脚本。类似这样的内容可能很接近,但我没有指向任何特定页面。
这就是我删除查询字符串的方式:
<rule name="Subject redirect with query" stopProcessing="true">
<match url="^(.*)" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
总结一下,这个怎么转:
https://www.example.com/fancy-url
至此
https://www.example.com/fancy-url?utm_source=Targeted_Email&utm_medium=email&utm_campaign=campaigntag
经过多次挫折,我终于弄明白了。查询字符串没问题,但其中一项设置阻止了其余规则的处理。在规则名称之后,我将停止处理设置为 true。当我在 IIS 中查看它时,该设置更具描述性。它说 "Stop processing of subsequent rules"。一旦我删除它并将其移到我的其他规则之上,就完美地工作了。
<rule name="Google Query string">
<match url="(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Rewrite" url="{R:0}" appendQueryString="false" />
</rule>