How do I create a rule that will fire only on my site's home page?

北城余情 提交于 2019-12-07 03:06:00

问题


I using Google Tag Manager and I am trying to setup a rule that will fire ONLY on my sites homepage.

The issue is that I am not certain how to handle all of the URL permutations of the homepage. How can I create a rule that will handle:

"http://" "https://" "http://www." "https://www."

Also, we use Sitecore and support multiple languages, so the homepage url can also display as:

"http://www.mysite.com/en"

I am not sure how to handle the culture identifier that is inserted into the URL path after a visitor has used the navigation on the site.

Is it possible to use the OOTB Google Tag Manager rules to handle this scenario, or will I have to implement a Tag Manager Data Layer?


回答1:


The following rule would check if it's the homepage:

{{url}} matches RegEx ^https?://(www\.)?mysite\.com/?(index\.html)?$

{{url}} gives the whole address whereas {{url domain}} just gives the domain and {{url path}} just the path (including the initial forward slash).

This matches http and https, with or without www and with or without index.html at the end. It also matches mysite.com/ and mysite.com (without the forward slash at the end). If you want to check for URL permutations at the end of the homepage, you could do something like:

^https?://(www\.)?mysite\.com/?(en|es|fr)?$ etc.

Also, forward slashes do NOT have to be escaped. In fact, escaping forward slashes broke the firing rule in GTM for me...

See http://en.wikipedia.org/wiki/Regular_expression

edit: and if you want to ignore the querystring (which is a good thing to do because most ads add query keys such as utm_source etc. to the url), you can have something like this:

^https?://(www\.)?mtsite\.com/?(index\.html)?/?(\?.*)?$

(note the (\?.*)? at the end)




回答2:


Ok... so after researching the Google Tag Manager Forum, this can be accomplished by making separate url "ends with" rules for your site url and then your site url with a trailing forward slash such as:

rule 1 : url ends with http://mysite.com

rule 2 : url ends with http://mysite.com/

I think it was the trailing slash that was confusing the matter as I was setting up the rules.




回答3:


This is a super robust way to know it's your home page with either protocol and allow for a backslash all with one line of Regex...

^https?://www.mydomain\.com\/?$

Not sure sure why the initial double backslashes don't have to be escaped, but it works. Would have expected ^https?:\/\/www.mydomain\.com\/?$ Maybe someone else knows that :)



来源:https://stackoverflow.com/questions/15575145/how-do-i-create-a-rule-that-will-fire-only-on-my-sites-home-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!