Below, you’ll find a detailed guide on how to add the WP Analytify 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 Wp Analytify Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Wp Analytify Plugin and the shortcodes it provides:
"Analytify – Google Analytics Dashboard For WordPress (GA4 made easy) is a powerful plugin that effortlessly integrates Google Analytics with your WordPress site. Track and view your site's performance directly from your dashboard with wp-analytify."
- [analytify_user_optout]
- [analytify_user_optin]
WP Analytify [analytify_user_optout] Shortcode
The ‘analytify_user_optout’ shortcode from WP-Analytify plugin allows users to opt-out from Google Analytics tracking. It checks if the user has a valid Google Analytics profile and renders an opt-out link.
Shortcode: [analytify_user_optout]
Examples and Usage
Basic example – A standard usage of the shortcode that initiates the opt-out process for Google Analytics tracking by the Analytify plugin.
[analytify_user_optout /]
PHP Function Code
In case you have difficulties debugging what causing issues with [analytify_user_optout]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'analytify_user_optout', array( $this, 'user_optout_shortcode' ) );
Shortcode PHP function:
function user_optout_shortcode( $atts, $content = '' ) {
$UA_CODE = WP_ANALYTIFY_FUNCTIONS::get_UA_code();
$is_authenticate_in = get_option( 'pa_google_token' );
if ( ! $UA_CODE || ! $is_authenticate_in ) {
if ( current_user_can( 'manage_options' ) ) {
return ' "Analytify Profile is not selected" ';
} else {
return '<!-- Analytify Profile is not selected. -->';
}
}
ob_start();
?>
<script>
var analytify_optout_string = 'ga-disable-' + '<?php echo $UA_CODE; ?>';
if ( document.cookie.indexOf( analytify_optout_string + '=true' ) > -1 ) {
window[ analytify_optout_string ] = true;
}
function analytify_analytics_optout() {
var exp_date = new Date;
exp_date.setFullYear(exp_date.getFullYear() + 10);
document.cookie = analytify_optout_string + '=true; expires=' + exp_date.toGMTString() + '; path=/';
window[ analytify_optout_string ] = true;
<?php echo apply_filters( 'analytiy_user_optout_message', '' ); ?>
}
</script>
<?php
$output = ob_get_contents();
ob_end_clean();
if ( '' == $content ) {
$content = __( 'Click here to opt-out.', 'wp-analytify' );
}
$output .= '<a class="analytify-opt-out" href="javascript:analytify_analytics_optout();">' . $content . '</a>';
return $output;
}
Code file location:
wp-analytify/wp-analytify/classes/user_optout.php
WP Analytify [analytify_user_optin] Shortcode
The WP-Analytify shortcode ‘analytify_user_optin’ enables users to opt-in to Google Analytics tracking. Upon activation, it checks if the Analytify Profile is selected and the Google token is authenticated. If not, it displays a message to the admin. If authenticated, it sets a cookie to opt the user into Analytics tracking. The shortcode also allows customization of the opt-in message.
Shortcode: [analytify_user_optin]
Examples and Usage
Basic example – The shortcode ‘analytify_user_optin’ can be used to allow users to opt-in for Google Analytics tracking. By default, it displays a link with the text ‘Click here to opt-in.’.
[analytify_user_optin]
Advanced examples
Modifying the default text of the opt-in link. You can replace ‘Your custom text here’ with any text you want to display.
[analytify_user_optin]Your custom text here[/analytify_user_optin]
Adding a custom opt-in message using the ‘analytiy_user_optin_message’ filter. This message will be displayed when a user opts in.
add_filter( 'analytiy_user_optin_message', function() {
return 'Thank you for opting in!';
});
Then use the shortcode as normal:
[analytify_user_optin]
Note: The ‘analytiy_user_optin_message’ filter should be added to your theme’s functions.php file or a custom plugin.
PHP Function Code
In case you have difficulties debugging what causing issues with [analytify_user_optin]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'analytify_user_optin', array( $this, 'user_optin_shortcode' ) );
Shortcode PHP function:
function user_optin_shortcode( $atts, $content = '' ) {
$UA_CODE = WP_ANALYTIFY_FUNCTIONS::get_UA_code();
$is_authenticate_in = get_option( 'pa_google_token' );
if ( ! $UA_CODE || ! $is_authenticate_in ) {
if ( current_user_can( 'manage_options' ) ) {
return ' "Analytify Profile is not selected" ';
} else {
return '<!-- Analytify Profile is not selected. -->';
}
}
ob_start();
?>
<script>
var analytify_optout_string = 'ga-disable-' + '<?php echo $UA_CODE; ?>';
function analytify_analytics_optin() {
var exp_date = new Date;
exp_date.setFullYear(exp_date.getFullYear() - 30);
document.cookie = analytify_optout_string + '=true; expires=' + exp_date.toGMTString() + '; path=/';
window[ analytify_optout_string ] = true;
<?php echo apply_filters( 'analytiy_user_optin_message', '' ); ?>
}
</script>
<?php
$output = ob_get_contents();
ob_end_clean();
if ( '' == $content ) {
$content = __( 'Click here to opt-in.', 'wp-analytify' );
}
$output .= '<a class="analytify-opt-in" href="javascript:analytify_analytics_optin();">' . $content . '</a>';
return $output;
}
Code file location:
wp-analytify/wp-analytify/classes/user_optout.php
Conclusion
Now that you’ve learned how to embed the Wp Analytify 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.
Leave a Reply