Skip to content

Instantly share code, notes, and snippets.

@mucahitnezir
Last active July 18, 2019 12:58
Show Gist options
  • Save mucahitnezir/66ad071386f6f0328c5682306d9cdd14 to your computer and use it in GitHub Desktop.
Save mucahitnezir/66ad071386f6f0328c5682306d9cdd14 to your computer and use it in GitHub Desktop.
Googe Tag Manager integration for wordpress websites. Replace your GTM code with YOUR-GTM-CODE in functions.php. Copy the contents of these files to active wordpress theme in your wordpress website.
<?php
/* Google Tag Manager Code For Head Tag */
add_action('wp_head', 'wp_head_tag_manager');
function wp_head_tag_manager()
{
?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','YOUR-GTM-CODE');</script>
<!-- End Google Tag Manager -->
<?php
}
/* Google Tag Manager Code For Body Tag */
add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
function custom_content_after_body_open_tag()
{
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=YOUR-GTM-CODE"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
<!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<!-- THIS IS IMPORTANT TO INSERT GOOGLE TAG MANAGER CODE -->
<?php do_action('after_body_open_tag'); ?>
<!-- OTHER HTML CODES -->
@StefsterNYC
Copy link

Your functions hook doesn't seem to be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment