On this page
- 1 Add Tracking Code in WordPress
- 2 Add Tracking Code in WordPress
- 2.1 Meta Pixel Code
- 2.2 Google Tag Manager
- 2.2.0.1 Metadata in WordPress
- 2.2.0.2 WP_List_Table tutorial : How to display data from database to the admin block ?
- 2.2.0.3 Shortcode in WordPress
- 2.2.0.4 WordPress: Post Types
- 2.2.0.5 Creating an Administrator Account in WordPress Using Custom PHP Code
- 2.2.0.6 WordPress: Plugins
- 2.2.0.7 WordPress: Hooks
- 2.2.0.8 WordPress: Important Functions
- 2.2.0.9 Cheatsheet: WordPress
- 2.3 Leave A Comment Cancel reply
Add Tracking Code in WordPress
Add Tracking Code in WordPress
July 12, 2023
Meta Pixel Code
The Meta Pixel is a snippet of JavaScript code that allows you to track visitor activity on your website. It works by loading a small library of functions which you can use whenever a site visitor takes an action (called an event) that you want to track (called a conversion).
Google Tag Manager
Google Tag Manager works by using a single JavaScript code snippet that you add to your site as a container for all the tags you want to manage.
<?php function sw_custom_js() { echo ' <!-- Your Tracking Code --> <!-- End Tracking Code -->'; } // Add hook for admin <head></head> add_action('admin_head', 'sw_custom_js'); // Add hook for front-end <head></head> add_action('wp_head', 'sw_custom_js');
// Add Google Tag code which is supposed to be placed after opening body tag. add_action( 'wp_body_open', 'wpdoc_add_custom_body_open_code' ); function wpdoc_add_custom_body_open_code() { echo '<!-- Google Tag Manager (noscript) --><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-J4LMVLR" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><!-- End Google Tag Manager (noscript) -->'; }