---
title: Add custom code before the closing tag of the gtag script
url: "https://www.seopress.org/support/hooks/add-custom-code-before-the-closing-tag-of-the-gtag-script/"
lang: en-US
updated: 2026-10-20
hook_name: seopress_gtag_before_closing_script
required_version: 7.5.1
---

# Add custom code before the closing tag of the gtag script

- **Hook name:** `seopress_gtag_before_closing_script`
- **Required version:** 7.5.1

```php
<?php
/**
 * Append JavaScript to the Google Analytics gtag script, just before it is closed.
 *
 * @param string $script JavaScript code, empty by default.
 * @return string
 */
function sp_gtag_before_closing_script( $script ) {
	$script .= "\ngtag('set', 'user_properties', { site_type: 'blog' });";

	return $script;
}
add_filter( 'seopress_gtag_before_closing_script', 'sp_gtag_before_closing_script' );
```

Source: [https://gist.github.com/wp-seopress/a4eb59c66fcdba70d4f93a9c1e4e9ac4](https://gist.github.com/wp-seopress/a4eb59c66fcdba70d4f93a9c1e4e9ac4)



