Help - Search - Members - Calendar
Full Version: Using Category Variables To Populate Javascript
Movable Type Community Forum > Using Movable Type > Categories and Entry Tags
rob_m
I'm trying to populate javascript variables that are fed to a third party stats package. For example:

site_category="<$MTCategoryLabel$>"

would allow me to view the traffic breakdown by category.

The javascript resides in my header template, and when I use this approach, I get the error message "You used <$MTCategoryLabel$> outside of the proper context". I've also tried <$MTEntryCategory$> with the same result.

Has anybody got any idea as to how I might get around this? I'm relatively new to the platform so well aware I could be barking up completely the wrong tree.

Thanks!
Su-
QUOTE (rob_m @ May 23 2008, 08:59 AM) *
site_category="<$MTCategoryLabel$>"
The javascript resides in my header template, and when I use this approach, I get the error message "You used <$MTCategoryLabel$> outside of the proper context".

If that header is used across your entire site, this is correct. Think of it like this: What category is your main index in? The question doesn't even make sense. The doc page for CategoryLabel will show you where this tag is acceptable.

QUOTE (rob_m @ May 23 2008, 08:59 AM) *
I've also tried <$MTEntryCategory$> with the same result.
Pretty much the same thing here. This tag expects to appear in an entry context(eg. individual archive, or within an Entries loop.) So what's the entry_id of your "blah" category archive? Right.

As far as your code snippet and what you're trying to get done, it looks like you're trying to attach a category to your entire blog, and there's no such thing as a blog-level category field, so first what exactly do you expect to be showing up in there, and where are you expecting it to come from? And is that "site category" going to change so much you can't just hard-code this? There are easy ways to simply make your snippets shut up and work, but I don't know that they'd really make much sense, since the value would keep changing.
rob_m
Thanks for your reply Su! Perhaps I need to go back to basics here... from what you say, I think I'm barking up the wrong tree with using MTCategoryLabel.

I'm putting some third party tracking code into a blog, and yes the code has gone into my header template which is used across the site. I want to insert the name of the category into this code. If an article resides within a category I want to use that category's name. Do you understand what I'm trying to achieve? And is there any way to do it?

Perhaps I'm missing some of the fundamentals of the MT platform - I'm not particularly experienced in it - but any help would be hugely appreciated.

Thanks!

Rob
Su-
Okay:
  • You have a chunk of script for stats tracking
  • ...which is shared across the site
  • ...but IF the current document belongs to a category, you want to include that additional bit of information.

Yes?

Also, could you paste in the code in question? (Assuming it's not confidential.) It'll give me an idea what I have to work with.
rob_m
here's a simplified version of what I've got...


<script language="JavaScript" type="text/javascript"><!--
/* You may give each page an identifying name, server, and channel on
the next lines. */
s.pageName=document.title
s.server=document.domain
s.channel=""
s.hier1=""
s.pageType=""
s.prop1=""
s.prop2=""
s.prop3=""
s.prop4=""
s.prop5=" "
s.prop15=document.title
s.prop16=" "
/* Conversion Variables */
s.campaign=""
s.state=""
s.zip=""
</script>



so this code will sit between the <head> of every page, and I want to set s.channel="" to the category name.

thanks so much for your help so far with this.

rob



Su-
Okay, the condensed version is something like this:
CODE
[stuff]

s.channel="<mt:IfArchiveType type="Individual"><$mt:EntryCategory encode_js="1"$></mt:IfArchiveType><mt:IfArchiveType type="Category"><$mt:CategoryLabel encode_js="1"$></mt:IfArchiveType>"

[rest of stuff]

...which will make MT only try to output a category name if it's currently dealing with an entry or category archive, and not an index or date-based archives(which can't have categories).

Note this is untested(Sorry; I've got clients.), so there's a chance it'll fail, but should at least give you a conceptual framework for what you need to do.
rob_m
thank you su - i'll give this a go when i get into the office tomorrow and let you know how i get on.

rob
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.