Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Google Tag Manager #582

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/app_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function beforeFilter()
$this->set('trackingId', $trackingId);
$this->set('domain', $domain);
$this->set('customLogo', $customLogo);


// for setting up google tag manager
$gtmContainerId = $this->SysParameter->findByParameterCode('google_tag_manager.container_id');
$this->set('gtmContainerId', $gtmContainerId);

parent::beforeFilter();
}

Expand Down
12 changes: 12 additions & 0 deletions app/config/sql/delta_16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--
-- Canvas integration
--

INSERT INTO `sys_parameters` (
`parameter_code`, `parameter_value`,
`parameter_type`, `description`, `record_status`, `creator_id`, `created`,
`updater_id`, `modified`)
VALUES (
'google_tag_manager.container_id', '', 'S',
'Container ID for Google Tag Manager (GTM-XXXX)', 'A', 0, NOW(), NULL, NOW()
);
28 changes: 27 additions & 1 deletion app/views/layouts/default.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,36 @@

// Custom View Include Files
echo $scripts_for_layout;

// check that a google tag manager container id is given
$gtmHead = null;
$gtmBody = null;
if (!empty($gtmContainerId) && !empty($gtmContainerId['SysParameter']['parameter_value'])) {
$gtmContainerId = $gtmContainerId['SysParameter']['parameter_value'];

$gtmHead = "
<!-- 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','$gtmContainerId');</script>
<!-- End Google Tag Manager -->
";
$gtmBody = '
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id='.$gtmContainerId.'"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
';
}

echo $gtmHead;
?>
</head>

<body>

<?php echo $gtmBody; ?>
<div class='containerOuter pagewidth'>
<!-- BANNER -->
<?php echo $this->element('global/banner', array('customLogo' => $customLogo)); ?>
Expand Down