How to add conditions to scripts in masterpages

This forum is for all Flare related Tips and Tricks.
Have a tip or trick you use while working in Flare? Share it here.

How to add conditions to scripts in masterpages

Postby sdriggars on Mon Nov 04, 2013 12:03 pm

Hello! I am hoping someone has a tip that can help me accomplish this task.

We've been asked to include Google Analytics scripts with our Help outputs. There are separate scripts for each output. I want these scripts in my masterpages so I don't have to manually add them to each individual topic.

However, the script information is the the head section of the masterpage. I don't see a way to add conditional tags to content in the head section. In the XML Editor, with structure bars turned on, I can right-click on the HTML bar and add conditions, but that applies to the contents of the body section of the masterpage, not the head. Perhaps I can hand code the conditional tags for each script, but I was hoping to be able to use the structure bars for simplicity, and I'm concerned that the fact that conditonal tagging isn't available in the head section means that it won't work there.

Any suggestions?

--Stephen
--Stephen
sdriggars
Propeller Head
 
Posts: 25
Joined: Wed Sep 03, 2008 2:00 pm

Re: How to add conditions to scripts in masterpages

Postby kwag_myers on Tue Nov 05, 2013 5:58 am

I'm not sure I understand why you want to use conditional tags, but you can apply conditional tagging to your Masterpages. This means you'd have to have a separate Masterpage for each tag and assign the corresponding topics to the Masterpage (which would be about as much work as tagging each topic).

I wonder if you can use a third-party search and replace tool, like Search and Replace Master, to insert the code into the header of each topic?
"I'm tryin' to think, but nothin' happens!" - Curly Joe Howard
kwag_myers
Propellus Maximus
 
Posts: 810
Joined: Wed Jul 25, 2012 11:36 am
Location: Ann Arbor, MI

Re: How to add conditions to scripts in masterpages

Postby sdriggars on Tue Nov 05, 2013 9:10 am

Thanks!

I only want to put conditions on a section of the masterpage, not the entire page. There are other components of the masterpage that I want to be available in all topics. I want the condition to only apply to specific target outputs so that I can build each output with a specific script included without having to manually change it each time.
--Stephen
sdriggars
Propeller Head
 
Posts: 25
Joined: Wed Sep 03, 2008 2:00 pm

Re: How to add conditions to scripts in masterpages

Postby LTinker68 on Tue Nov 05, 2013 1:55 pm

So what about making multiple copies of the masterpage, changing the script as appropriate in each version, then pointing each target to its appropriate masterpage?
Image

Lisa
Eagles may soar, but weasels aren't sucked into jet engines.
Warning! Loose nut behind the keyboard.
LTinker68
Master Propellus Maximus
 
Posts: 7245
Joined: Thu Feb 16, 2006 9:38 pm

Re: How to add conditions to scripts in masterpages

Postby Msquared on Tue Nov 05, 2013 4:30 pm

Any solution that involves taking multiple copies has the potential to become a maintenance headache. If you need to change anything that is duplicated in multiple topics or master pages you will have to remember to do them all.

I can't remember if I've tried setting conditions in master page headers. I know you can use variables in page headers, and that might work for you. You could try using the variable in the master page, then, for each target, set the variable to the text of the script that you want. You may find that easier to do by editing the .fltar file directly in a text editor, as the interface to set variable values on the Variables tab of the target file is rather restrictive. It will also help if your script is short!
Marjorie

My goal in life is to be as good a person as my dogs already think I am.
Msquared
Propellus Maximus
 
Posts: 848
Joined: Mon Aug 06, 2012 10:19 am
Location: Southampton, UK

Re: How to add conditions to scripts in masterpages

Postby sdriggars on Tue Nov 05, 2013 5:46 pm

Lisa--thanks for the suggestion! I had considered that, but I was hoping to avoid multiple master pages for the reasons Msquared mentions.

Msquared--the variable option is an interesting one. I'll need to play with that a bit to see if it works.

Thanks!

--Stephen
--Stephen
sdriggars
Propeller Head
 
Posts: 25
Joined: Wed Sep 03, 2008 2:00 pm

Re: How to add conditions to scripts in masterpages

Postby nielsf on Tue Jan 10, 2017 4:13 am

So here's how I solved this old problem. We use Piwik, but it might as well be Google Analytics.
We want to get statistics from three sites. Each site needs a separate Piwik code.
So I created three snippets, each with a complete Piwik script in it, but with different Piwik codes.
I tagged the snippets at file level with three combinations of conditions like "IncludeSite1 and ExcludeSite2 and ExcludeSite3", etc.
I then added all three snippets in all relevant master pages.
This works fine. I could not get Flare to expand a variable within a script. And pasting a whole script (even if on one line) into a variable hard-crashed Flare :shock: .

-Niels
-----
Flaring in Copenhagen, Denmark
nielsf
Propeller Head
 
Posts: 86
Joined: Mon Feb 02, 2015 5:44 am
Location: Copenhagen, Denmark

Re: How to add conditions to scripts in masterpages

Postby Dave Lee on Tue Jan 10, 2017 5:15 am

We use Google Analytics in our projects, and I use conditions to manage this.

* For each GA tracking ID, I put the GA script into an individual *.js file. Typically, I need one of these per target.
* I have a single snippet that contains links to all the GA script files (for all targets).
* In the snippet, I set a condition on each script file link - the appropriate conditions are excluded in each target.
* I insert the snippet into the body of my master pages.

So my snippet looks like this:

Code: Select all
    <body>

            <MadCap:conditionalText MadCap:conditions="Target.ProductA">
                <script src="../Scripts/GA-ProductA.js"></script>
            </MadCap:conditionalText>

            <MadCap:conditionalText MadCap:conditions="Target.ProductB">
                <script src="../Scripts/GA-ProductB.js"></script>
            </MadCap:conditionalText>

    </body>
Dave Lee
Master Propellus Maximus
 
Posts: 5657
Joined: Mon Mar 05, 2007 8:33 am
Location: UK


Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 2 guests