I have found several posts asking for custom head code per page but none with a solid solution, so I’m hoping I have just missed the aha moment post somehow …
ISSUE
Our site represents franchisees, each of which has their own location page, and many of them want to add analytics tags to ONLY their pages, not the whole site. … and I am stumped on how to add custom code to just their pages within the /head tag and just before the /body tag.
I’m OK using something like this in the global /head / body areas, but have yet to be able to make it work…
if (window.location.pathname == ‘RELATIVE-PATH-TO PAGE’)
{
CODE HERE
}
Custom code can be added to the head of the document on any particular page. That option does not exist on collection items since there is only one collection template.
You can run code that adds features or additional resources to a page based on the URL with JavaScript. You just have to write it or hire someone to code it for you.
@WJM - you could also possible include a field or a field in combination with a toggle field to allow individual locations to insert their own UA id on their page.
For example you could have a text field called Google Analytics ID where users can insert a value. Then in the custom code for the page you can insert that variable and use JS to insert the value dynamically in the analytics code.
I forgot about the toggle option … something like that might work.
I’ll take a stab at it, but won’t the Google JS load with an empty value for pages with the field ‘off’?
Depending on what other dynamic content you need to load on each franchisee’s landing page, could you build those as regular pages with collection inserts instead of using a collection template? Then you could have the head code available natively.