google analytics - How to detect if a Shortcode is being called on a page?

I'm trying to push some variables to google analytics and would like to be able to detect if a shortcode has been i

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

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 asked Oct 1, 2011 at 11:36 Noel TockNoel Tock 8812 gold badges8 silver badges21 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

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 is in 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?

For anyone who came to this question here is the correct answer:

Use has_shorcode() to detect if a specific shortcode was called. Can be viewed here:

https://developer.wordpress/reference/functions/has_shortcode/

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/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
        //]]>
    </script>
    <?php
}

A simple way, use $tag variable within your shortcode function. Get this idea from here

add_shortcode("your_shortcode_tag", "shortcode_function");

function shortcode_function($atts, $content = null, $tag){ // $tag variable is here
   // check if your shortcode is called
    if($tag == "your_shortcode_tag"){
        // do your stuff
    }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744849103a4597018.html

相关推荐

  • google analytics - How to detect if a Shortcode is being called on a page?

    I'm trying to push some variables to google analytics and would like to be able to detect if a shortcode has been i

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信