Skip to content

Instantly share code, notes, and snippets.

@deeman
Last active July 11, 2020 17:22
Show Gist options
  • Select an option

  • Save deeman/8e44babf91cd3a57731d640b5cd4226e to your computer and use it in GitHub Desktop.

Select an option

Save deeman/8e44babf91cd3a57731d640b5cd4226e to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WordPress Simple Google Analytics Plugin
Description: Lägger till Google Analytics-kod i <head> eller <footer> genom WordPress-hook
* Version: 1.0
* Author: D. Andersson
* Author URI: https://dandersson.nu
*/
defined( 'ABSPATH' ) or exit;
function da_google_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
<?php }
add_action( 'wp_head', 'da_google_analytics', 10 );
echo $analytics;
}
@deeman

deeman commented Jul 11, 2020 •

Copy link
Copy Markdown
Author

If needed, you can set it use footer by changing line 23 to
add_action( 'wp_footer', 'da_google_analytics', 10 );

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