Support » Fixing WordPress » How to add tracking code to individual page?

  • I would like to add a Google tracking code to a couple of individual pages, for example a “Thank You” page and “Checkout page”. I know how to add a Google Analytics code which will run throughout the whole website into the head tag, however I’m a bit confused as how to add a different tracking code to just one single page as I do not know how you access the source code of an individual page on wordpress.

    Any guidance/help would be appreciated. Thanks!

Viewing 1 replies (of 1 total)
  • Howdy @philippaeng,

    You could do this a few ways. You could add the tracking code to its’ own .js file and then enqueue it only on the required pages by using something like the following in your functions.php.

    Important: Create a child theme before making edits so you don’t lose your customizations when you update the theme!

    This is assuming you name the file tracking.js and it lives in a directory “js” in the root of your active theme. “thank-you” and “checkout” would be the slug of page you are trying to include, you could also pass the page ID instead if you prefer.

    function my_tracking_script() {
    	if ( is_page( 'thank-you' ) || is_page( 'checkout' ) ) {
    		wp_enqueue_script( 'tracking-code', get_template_directory_uri() . '/js/tracking.js' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'my_tracking_script' );
Viewing 1 replies (of 1 total)
  • The topic ‘How to add tracking code to individual page?’ is closed to new replies.