简体   繁体   中英

Google Tag Manager (GTM) and Google Analytics variables

Is it possible to get a tag fired by a javascript rule, when the value the rule based on is inside the _gaq javascript?

This is my Google Analytics javascript:

<script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-960143-1']);
            _gaq.push(['_setDomainName', '.immoscout24.ch']);
            _gaq.push(['_trackPageview', '/ContactFormSent.html']);
            _gaq.push(['_trackPageLoadTime']);
            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();
            </script>

The Google Tag Manager help says this:

JavaScript Variable: The value is set to that of the global variable you specify. If the value you're looking for was not set up in the data layer (see Data Layer macro type, below), and it's not visible in the DOM (see DOM macro types, below), it's possible that the value can be retrieved from a JavaScript variable. Use this macro type if you can find the value you're looking for in the source of the page in the form of a JavaScript variable.

Now I want to have a macro for the _trackPageview and a rule for the /ContactFormSent.html based on this macro. So the variable should be global --> is this the case with '_trackPageview' ? If it is, it should be possible to fire the tag based on this rule.

Or is something with the setup inside of the Google Tag Manager wrong?

  • macroname: trackPageview
  • macrotype: JavaScript-variable
  • name of the global variable: _trackPageview

Thx for helping me out :-) Fabian

It's possible to use _gaq, only it's a lot less convenient than you seem to think. _gaq is an array and every item added via the push-method in the GA code is an array as well. So the way to adress _trackPageview in your example would be _gaq[2][0] (first item in the third array) - at least until somebody adds another "push" to the GA code in which case your GTM rules will break. So possible, yes, good idea, not so much.

(To clarify a little more, your error was to think that _gaq is a one-dimensonal array with key=>value pairs. It isn't).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM