ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Strategy to serve up Google Analytics based on channel...

April 23, 2012 3:53pm

Subscribe [3]
  • #1 / Apr 23, 2012 3:53pm

    Doy

    38 posts

    I’m hoping I’ll explain this well…not sure how to figure it out yet.

    I have a “main_pages” channel (using the Pages module) that grabs from about 10 or so other channels and serves them up on each page. I’ve created a couple of different template types, one of which is a “landing page” template that allows a user to paste Google Analytics tracking code into the page so that we can track that particular page in analytics (as part of a goal, etc.).

    So basically, if the channel is “main_pages” I’m going to query whether the the {pages_google} field is populated, and if not, serve the standard Google Analytics code.

    So this code is going to be in a footer that is shared across all templates. How do I get templates that aren’t part of the “main_pages” channel to ignore this code and allow all the other pages to use the standard analytics code?

    I was looking at the conditional variables, and didn’t see anything that would let me make a conditional based on channel. Is there a way to do this? Or do I just have to break down and create a second footer (BLEH!)?

  • #2 / Apr 24, 2012 4:18pm

    Rob Allen

    3114 posts

    You could do this with URL segments if you have a suitable URl structure.

    Lets say your URL is http://www.mysite.com/landing

    <!-- check to see if its a landing page -->
    {if segment_1 == "landing"}
    
      {exp:channel:entries channel="main_pages" limit="1"}
      
        <!-- check to see if the custom google code is present -->
        {if pages_google != ""}
        {pages_google}
        {/if}
    
        <!-- if custom code not present add default google code -->
        {if pages_google == ""}
        Default google code
        {/if}
    
      {/exp:channel:entries}
    {/if}
    
    <!-- check to see if its NOT a landing page -->
    {if segment_1 != "landing"}
     Default google code
    {/if}


    Alternative method:

    {if segment_1 == "landing"}
    
      {exp:channel:entries channel="main_pages" limit="1" search:pages_google="not IS_EMPTY"}
      
        {pages_google}
    
        {if no_results}
        Default google code
        {/if}
    
      {/exp:channel:entries}
    
    {/if}
    
    {if segment_1 !="landing"}
     Default google code
    {/if}
  • #3 / Apr 25, 2012 7:01am

    John St-Amand

    865 posts

    You can use {if channel_short_name == "landing"}do this{/if} as well, though if your segment has a one to one relationship with the channel, blue dreamer’s suggestion is perhaps the more practical way to achieve this because the condition is run before the entries loop is used.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases