Google Analytics For WordPress MonsterInsights Shortcodes

Below, you’ll find a detailed guide on how to add the Google Analytics For WordPress Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Google Analytics For WordPress Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Google Analytics For WordPress Plugin and the shortcodes it provides:

Plugin Icon
MonsterInsights – Google Analytics Dashboard for WordPress (Website Stats Made Easy)

"MonsterInsights is a user-friendly WordPress plugin that integrates Google Analytics into your website. It simplifies tracking website stats, making data management easy and efficient."

★★★★✩ (2710) Active Installs: 3000000+ Tested with: 6.3.2 PHP Version: 7.2
Included Shortcodes:
  • [gadwp_useroptout]
  • [render_badge]
  • [monsterinsights_popular_posts_shortcode_output]

Google Analytics For WordPress [gadwp_useroptout] Shortcode

The Google Analytics for WordPress shortcode ‘gadwp_useroptout’ is used to return an empty string. This shortcode doesn’t perform any action or display content. It’s often used when you want to ensure no output is returned from a specific part of your code.

Shortcode: [gadwp_useroptout]

Examples and Usage

Basic example – The shortcode ‘gadwp_useroptout’ is used to return an empty string in the context of Google Analytics for WordPress. It can be used when you want to stop tracking for a specific user or a group of users.

[gadwp_useroptout /]

PHP Function Code

In case you have difficulties debugging what causing issues with [gadwp_useroptout] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'gadwp_useroptout', '__return_empty_string' );

Code file location:

google-analytics-for-wordpress/google-analytics-for-wordpress/includes/frontend/frontend.php

Google Analytics For WordPress [render_badge] Shortcode

The Google Analytics for WordPress shortcode is a tool to display a verified badge on your site. The badge’s appearance can be ‘light’ or ‘dark’, and its position ‘left’, ‘center’, or ‘right’. It links to the MonsterInsights site, showcasing your site’s verification.

Shortcode: [render_badge]

Parameters

Here is a list of all possible render_badge shortcode parameters and attributes:

  • appearance – controls the look of the badge, can be either ‘light’ or ‘dark’
  • position – determines the alignment of the badge, can be ‘left’, ‘center’, or ‘right’

Examples and Usage

Basic example – The basic usage of this shortcode will display the MonsterInsights badge in a light appearance and centered on the page.

[monster_insights_badge /]

Advanced examples

Displaying the MonsterInsights badge with a dark appearance and aligned to the left.

[monster_insights_badge appearance="dark" position="left" /]

Displaying the MonsterInsights badge with a light appearance and aligned to the right.

[monster_insights_badge appearance="light" position="right" /]

Please note that the shortcode might be different based on your plugin settings. The shortcode key (in this case ‘monster_insights_badge’) is defined in the plugin’s code and could be different. The same applies to the shortcode attributes ‘appearance’ and ‘position’.

PHP Function Code

In case you have difficulties debugging what causing issues with [render_badge] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode($this->shortcode_key, array($this, 'render_badge'));

Shortcode PHP function:

function render_badge($atts)
	{
		$atts = shortcode_atts(array(
			'appearance' => 'light',
			'position'   => 'center'
		), $atts, $this->shortcode_key);

		$img_src =  esc_url(plugins_url('assets/images/monsterinsights-badge-' . (in_array($atts['appearance'], array('light', 'dark'), true) ? $atts['appearance'] : 'light') . '.svg', MONSTERINSIGHTS_PLUGIN_FILE));

		return sprintf(
			'<div style="text-align: %1$s;"><a href="%2$s" target="_blank" rel="nofollow"><img style="display: inline-block" alt="%3$s" title="%3$s" src="%4$s"/></a></div>',
			(in_array($atts['position'], array('left', 'center', 'right'), true) ? $atts['position'] : 'center'),
			$this->get_link(),
			__('Verified by MonsterInsights', 'google-analytics-for-wordpress'),
			$img_src
		);
	}

Code file location:

google-analytics-for-wordpress/google-analytics-for-wordpress/includes/frontend/verified-badge/Controller.php

Google Analytics For WordPress [monsterinsights_popular_posts_shortcode_output] Shortcode

The Google Analytics for WordPress shortcode initiates the ‘render_shortcode’ function. It checks if arguments are empty or non-array, and if so, defaults them to an empty array. This shortcode applies filters to the output, allowing customization of popular posts displayed.

Shortcode: [monsterinsights_popular_posts_shortcode_output]

Examples and Usage

Basic example – The following shortcode will display the popular posts on your website, using the default settings.

[monsterinsights_popular_posts /]

PHP Function Code

In case you have difficulties debugging what causing issues with [monsterinsights_popular_posts_shortcode_output] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( $this->shortcode_key, array( $this, 'render_shortcode' ) );

Shortcode PHP function:

function render_shortcode( $args ) {
		if ( empty( $args ) || ! is_array( $args ) ) {
			$args = array();
		}

		return apply_filters( 'monsterinsights_popular_posts_shortcode_output', $this->shortcode_output( $args ), $args, $this );

	}

Code file location:

google-analytics-for-wordpress/google-analytics-for-wordpress/includes/popular-posts/class-popular-posts.php

Conclusion

Now that you’ve learned how to embed the Google Analytics For WordPress Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *