Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Depending on whether I am publishing an Umbraco site to my local dev machine or the client's test/production machine, I want to include a different bit of Google Tag Manager code.
Is there a way to conditionally include a specific script?
Umbraco has relieved nearly all my page editing nightmares, save for this one!
A common approach is to usually use a textbox multiple to save the code snippet (textarea) and then you can render the code using a macro, which means it will of course render different codes on different environments.
You can have to boxes one for scripts that need to go in the section of your page and then one for rendering those that need to be placed before .
That way your client has maximum freedom (and responsibility of course) and you don't risk messing things up yourself.
Hope this makes sense.
What you can do it is by creating a field in your Umbraco where the use can be able to write the whole script, or just the part that is a bit different bit.
Let me give you an example, normally I creates a tab on the homepage node called site settings in this tab I add a field called Google analytics id, this field has the type of textstring. Then user can add the google analytics key in it .
Then in the master template I add the script block for the google analytics, where the only thing I get from Umbraco is the google analytics id field.
If you want to give your user a field in Umbraco where they can paste whole scripts in, then I have good experience using the multiple textbox.
Hope this helps, and make sense. If you have other questions keep asking.
Great suggestions, thanks a lot!
Just to chime in.. I usually use a SiteSettings node with a footerJsCss text field to add sitewide code ( e.g. Google Analytics). Don't forget to use @Html.Raw if you're going to include <script> tags etc - I use the following in my Master template but you could of course do this on a page by page basis.
@* Get site settings node *@
var siteSettings = Model.Content.AncestorOrSelf(1).Siblings().Where("DocumentTypeAlias = \"SiteSettings\"").First();
In umbraco 6.2.5 verstion I am able add the script tags for each and every page.
from above image you can find the scripts tab.
but in umbraco 7.11.1 I could not able to see that feature. Is it possible to enable the feature if we can how it is possible.
In Umbraco 7, you would still go to your base page Document Type under the Settings section, and then add a Tab called Scripts, and add the field(s) for your injected JS/CSS there using Textarea DataTypes.
Then in your Partial View for your master template (where youre and tags are) you would render the content basically the same way as in Umbraco v6, except using the typed content version of your Model rather than accessing the property dynamically.
Hi Eric Wilkinson,
Thank you for your response.I have added the script tab as per you suggestion.
From above image i have added the script tab and one property as textarea to add the script tags. But the script tag is displaying for all modules under home. I need to add the different scripts to the different pages.
ex: I need to add one script for innovation for good page and i need to add some other script tag to community page.
please give me any suggestion. Thank you.
This will do exactly what you need. It's changed the document type (think of this as a blueprint for the types of content that a page can have) but each individual page using that doc type has it's own content data.
So just because the field appears on each page it stores its own copy of the data - does that make sense?
What it will require is for you either to add that script to every page that needs it or to change the logic for it so it finds the nearest script from it's parents (which is what I suspect you want).
If so, change the code Eric suggested to:
That true flag will search recursively up the tree until it finds a script.
Hi Steve Morgan,
I have used below line of code to get the values from Umbraco and it is working fine for all pages.
var script = Model.Content.GetProperty(customScript).Value;
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted