Skip to content

Commit

Permalink
Merge pull request #51 from mithredate/main
Browse files Browse the repository at this point in the history
Allow setting custom domains for serving gtm.js
  • Loading branch information
freekmurze committed Feb 26, 2024
2 parents 0032a39 + 1c1e631 commit e409b9a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 16 deletions.
16 changes: 15 additions & 1 deletion README.md
Expand Up @@ -23,7 +23,7 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh

Google Tag Manager allows you manage tracking and marketing optimization with AdWords, Google Analytics, et al. without editing your site code. One way of using Google Tag Manager is by sending data through a `dataLayer` variable in javascript after the page load and on custom events. This package makes managing the data layer easy.

For concrete examples of what you want to send throught the data layer, check out Google Tag Manager's [Developer Guide](https://developers.google.com/tag-manager/devguide).
For concrete examples of what you want to send through the data layer, check out Google Tag Manager's [Developer Guide](https://developers.google.com/tag-manager/devguide).

You'll also need a Google Tag Manager ID, which you can retrieve by [signing up](https://tagmanager.google.com/#/home) and creating an account for your website.

Expand Down Expand Up @@ -103,7 +103,18 @@ return [
* to that file here, and we will load it for you.
*/
'macroPath' => '',

/*
* The key under which data is saved to the session with flash.
*/
'sessionKey' => '_googleTagManager',

/*
* Configures the Google Tag Manager script domain.
* Modify this value only if you're using "Google Tag Manage: Web Container" client
* to serve gtm.js for your web container. Else, keep the default value.
*/
'domain' => 'www.googletagmanager.com',
];

```
Expand All @@ -117,6 +128,9 @@ return [
'id' => 'GTM-XXXXXX',
'enabled' => env('APP_ENV') === 'production',
'macroPath' => app_path('Services/GoogleTagManager/Macros.php'),
'sessionKey' => '_googleTagManager',
// Base domain used in your GTM server container
'domain' => 'gtm.yourdomain.com',
];
```

Expand Down
8 changes: 7 additions & 1 deletion resources/config/config.php
Expand Up @@ -6,7 +6,7 @@
* The Google Tag Manager id, should be a code that looks something like "gtm-xxxx".
*/
'id' => env('GOOGLE_TAG_MANAGER_ID', ''),

/*
* Enable or disable script rendering. Useful for local development.
*/
Expand All @@ -24,4 +24,10 @@
*/
'sessionKey' => env('GOOGLE_TAG_MANAGER_SESSION_KEY', '_googleTagManager'),

/*
* Configures the Google Tag Manager script domain.
* Modify this value only if you're using "Google Tag Manage: Web Container" client
* to serve gtm.js for your web container. Else, keep the default value.
*/
'domain' => env('GOOGLE_TAG_MANAGER_DOMAIN', 'www.googletagmanager.com'),
];
19 changes: 17 additions & 2 deletions resources/views/body.blade.php
@@ -1,3 +1,12 @@
<?php
/**
* @var bool $enabled
* @var string $id
* @var string $domain
* @var \Spatie\GoogleTagManager\DataLayer $dataLayer
* @var iterable<\Spatie\GoogleTagManager\DataLayer> $pushData
*/
?>
@if($enabled)
<script>
function gtmPush() {
Expand All @@ -10,7 +19,13 @@ function gtmPush() {
}
addEventListener("load", gtmPush);
</script>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ $id }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<noscript>
<iframe
src="https://{{ $domain }}/ns.html?id={{ $id }}"
height="0"
width="0"
style="display:none;visibility:hidden"
></iframe>
</noscript>
@endif

24 changes: 16 additions & 8 deletions resources/views/head.blade.php
@@ -1,10 +1,18 @@
<?php
/**
* @var bool $enabled
* @var string $id
* @var string $domain
*/
?>
@if($enabled)
<script>
window.dataLayer = window.dataLayer || [];
</script>
<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','{{ $id }}');</script>
<script>
window.dataLayer = window.dataLayer || [];
</script>
<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://{{ $domain }}/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ $id }}');
</script>
@endif
19 changes: 18 additions & 1 deletion src/GoogleTagManager.php
Expand Up @@ -19,6 +19,11 @@ class GoogleTagManager
*/
protected $enabled;

/**
* @var string
*/
protected $gtmScriptDomain;

/**
* @var \Spatie\GoogleTagManager\DataLayer
*/
Expand All @@ -36,10 +41,12 @@ class GoogleTagManager

/**
* @param string $id
* @param string $gtmScriptDomain
*/
public function __construct($id)
public function __construct($id, $gtmScriptDomain)
{
$this->id = $id;
$this->gtmScriptDomain = $gtmScriptDomain;
$this->dataLayer = new DataLayer();
$this->flashDataLayer = new DataLayer();
$this->pushDataLayer = new Collection();
Expand All @@ -64,6 +71,16 @@ public function setId($id)
return $this;
}

/**
* Return the Google Tag Manager script domain.
*
* @return string
*/
public function gtmScriptDomain()
{
return $this->gtmScriptDomain;
}

/**
* Check whether script rendering is enabled.
*
Expand Down
9 changes: 6 additions & 3 deletions src/GoogleTagManagerServiceProvider.php
Expand Up @@ -36,12 +36,15 @@ public function register()
$this->mergeConfigFrom(__DIR__.'/../resources/config/config.php', 'googletagmanager');

$this->app->singleton(GoogleTagManager::class, function($app) {
$googleTagManager = new GoogleTagManager(config('googletagmanager.id'));

$googleTagManager = new GoogleTagManager(
config('googletagmanager.id'),
config('googletagmanager.domain')
);

if (config('googletagmanager.enabled') === false) {
$googleTagManager->disable();
}

return $googleTagManager;
});
$this->app->alias(GoogleTagManager::class, 'googletagmanager');
Expand Down
1 change: 1 addition & 0 deletions src/ScriptViewCreator.php
Expand Up @@ -24,6 +24,7 @@ public function create(View $view)
$view
->with('enabled', $this->googleTagManager->isEnabled())
->with('id', $this->googleTagManager->id())
->with('domain', $this->googleTagManager->gtmScriptDomain())
->with('dataLayer', $this->googleTagManager->getDataLayer())
->with('pushData', $this->googleTagManager->getPushData());
}
Expand Down

0 comments on commit e409b9a

Please sign in to comment.