
An example migrating Google analytics to Google Tags Manager GAをGTMに移行する例
I have been scratching my head for a few hours on migrating GA to GTM this week. This project used google analytics and now customer wants GTM with more freedom and less dependent on our code.
Here’s the simplified steps I did the migration.
- Create a GTM account as well as a container.
- Add GTM setup up code snippet to your web site( I am working on a web site not a Android or IOS app).
- Remove GA code snippet.
- Setup GTM custom event as a trigger.
- Setup GTM variables so that GTM can receive GA event data.
- Setup GTM tag which refers the trigger and the variables you just created.
- Comment out your javascript code where you sent GA events. Push into GA event data into GTM dataLayer instead.
- Run GTM preview as debug mode and check GA events are triggered normally and correctly. (You may also need to check from GA platform to make sure events received and displayed as before.)
- If everything is OK, publish your GTM container.
Here comes the snapshots and code samples.





Web site sample code as below:
<!-- 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',"{{ config('google.gtm_container_id') }}");</script>
<script>
var ga_uid='{!! $ga_uid !!}';
window.dataLayer.push({
'ga_uid': window.ga_uid,
});
</script>
send_event: function (event) {
if (!event || !GAM.check_gtm_loaded()) {
return false;
}
// tracker = ga.getAll()[0];
// tracker.send({
// hitType: 'event',
// eventCategory: event.category,
// eventAction: event.action,
// eventLabel: event.label,
// eventValue: event.value
// });
window.dataLayer.push({
'eventCategory': event.category,
'eventAction': event.action,
'eventLabel': event.label,
'eventValue': (event.value) ? event.value : null,
'event':'OBP'
});
},
I just replaced GA’s GA event tracker with GTM dataLayer.
That’s all. E—asy~