Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to push some variables to google analytics and would like to be able to detect if a shortcode has been included on a page at all.

Anybody know a smart way going about this?

Thank you!

Noel

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I have sometimes wondered the same thing - whether wp maybe checked on save and kept a register etc

I had a quick look at the code, and it seems not.

There is however a global $shortcode_tags;

and wp has this function `function get_shortcode_regex() { global $shortcode_tags; $tagnames = array_keys($shortcode_tags); $tagregexp = join( '|', array_map('preg_quote', $tagnames) );

// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcodes()
return '(.?)\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';

}

used here to apply the shortcode: ` $pattern = get_shortcode_regex(); return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content);

`

My regex is not that great, but maybe you could do something with that?

Alternatively:

Then there is the brute force way - for each of the shortcode tags, 'simply' check if '['.$tag isin the content ? But then some developers / older plugins do their own funky filters on the content with comments or other custom tags.

So then, you could also check the wp global $wp_filter for

$wp_filter['the_content']

the contents should the functions called for "the content".

To be clever

You could maybe add an action yourself on post save/update, do the check for filters and shortcodes then and store something in a post-meta. Then all you have to do at display time is check the post-meta.

Whew... is it worth it?

share|improve this answer
    
Wow, slightly embarrassed now :) I actually put some code together in the meantime, but it's quite "bruttish" to say the least (in comparison to yours anyway). Thanks a ton for these ideas Anmari, I'll definitely have to have a play around with it. I'll post mine too (though I think for general purposes your ideas will work far better for people finding this page in the future). Thanks again! –  Noel Tock Oct 1 '11 at 14:17

So here's my trainwreck version of it (as it stands this minute, Anmari has given plenty of good ideas). It's not really flexible at all for anyone else's use either, but our hosted platform and how people use it is very flexible (makes my life easier):

function tfh_analytics() {

    $client_ua = trim(get_option( 'tf_ua_analytics' ));

    ?>
    <script type='text/javascript'>
        //<![CDATA[
        var _gaq = _gaq || [];
        // TF Aggregate Tracking
        _gaq.push(['_setAccount','UA-xxxxxxx-x']);
        _gaq.push(['_trackPageview'],['_trackPageLoadTime']);
        // Define Theme
        _gaq.push(['_setCustomVar',1,'Theme','<?php echo TF_THEME; ?>']);
        // Define Category
        <?php
        if ( have_posts() && ( is_page() || is_single() ) ) {
            $content = get_the_content(the_post());
            // Check if Front Page
            if ( is_front_page() ) { 
                if ( is_home() ) {
                $category = 'home-blog';
                } else {
                $category = 'home-static'; 
                }
            } else {
                // Check if Events
                if ( stripos( $content , '[tf-event') ) { $category = 'events'; }
                // Check if Food Menu
                if ( stripos( $content , '[tf-food') ) { $category = 'food-menu'; }
                // Check if Location
                if ( stripos( $content , '[tf-googlemaps') ) { $category = 'location'; }
                // Check if Blog
                if ( is_home() ) { $category = 'blog'; }
                }
            if ( !$category ) { $category = 'other'; } 
        }   
        ?>
        _gaq.push(['_setCustomVar',2,'Category','<?php echo $category; ?>']);
        // Define Yelp Bar
        _gaq.push(['_setCustomVar',3,'YelpEnabled','<?php if( get_option( 'tf_yelp_enabled' ) == true) { echo 'true'; } else { echo 'false';} ?>']);     
        // Define User Type
        _gaq.push(['_setCustomVar',4,'User','<?php if( is_user_logged_in() ) { echo 'logged_in'; } else { echo 'logged_out'; } ?>',1]);

    <?php if ( $client_ua != '' ) { ?>
        // Client Tracking
        _gaq.push(['b._setAccount','<?php echo $client_ua; ?>']);
        _gaq.push(['b._trackPageview']);
    <?php } ?>

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
        //]]>
    </script>
    <?php
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.