How do I create a rule that will fire only on my site's home page?
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\.)?mysite\.com/?(index\.html)?/?(\?.*)?$
(note the (\?.*)?
at the end)
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.
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 :)