kalnyc67 / analytics-issues

Automatically exported from code.google.com/p/analytics-issues
0 stars 0 forks source link

Global object initialized only once when tag included twice with different names #472

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Name of affected component: Web Tracking (analytics.js)

Issue summary:

When inserting analytics.js tag twice, with two different global object names, 
one of the global object function will be left untouched, still having the 
"i[r].q=i[r].q||[]).push(arguments)" body, resulting in malfunctionning object.

Steps to reproduce issue:
1. Make a simple web page that includes two different names

`
<html>
    <head>
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','first_namespace_ga');
        </script>
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','second_namespace_ga');
        </script>
    </head>
</html>
`

2. Inspect the value of the two global objects. Console output with GA Debug:

`
* Initializing Google Analytics. (analytics_debug.js:9)
* Initializing Google Analytics. (analytics_debug.js:9)
/!\ Tracking script already loaded. Abandoning initialization. 
(analytics_debug.js:9)

> first_namespace_ga
<- function (){
            (i[r].q=i[r].q||[]).push(arguments)} test.html:4
> second_namespace_ga
<- function (a){dd("Executing Google Analytics 
commands.");F(1);$.H[D]($,[arguments]);ge()} analytics_debug.js:59

`

3. Observe the different content for the two variables

Expected output:
`
> first_namespace_ga
<- function (a){dd("Executing Google Analytics 
commands.");F(1);$.H[D]($,[arguments]);ge()} analytics_debug.js:59
> second_namespace_ga
<- function (a){dd("Executing Google Analytics 
commands.");F(1);$.H[D]($,[arguments]);ge()} analytics_debug.js:59

`
Actual results:
What do you actually see after performing the above steps?
`
> first_namespace_ga
<- function (){
            (i[r].q=i[r].q||[]).push(arguments)} test.html:4
> second_namespace_ga
<- function (a){dd("Executing Google Analytics 
commands.");F(1);$.H[D]($,[arguments]);ge()} analytics_debug.js:59

`

Suggestion to fix:

The second time the tag is inserted (and rightfully not initialized a second 
time), the second global object should be nonetheless aliased to the first one.

Use case: 

In some configurations, an external js library, included but not managed by the 
website owner, wants to track events on the page.
To prevent namespace conflicts, the library has to choose a unique object name.
The library must include an insertion of analytics.js, since it might load 
before any other website analytics code.
But the website owner might decide independently to load the analytics.js tag 
too, with its own name.

Original issue reported on code.google.com by laur...@adyoulike.com on 1 Aug 2014 at 10:48

GoogleCodeExporter commented 9 years ago
Inserting the tracking code twice on a page like you've show will not work and 
is not supported. The reason you're getting the result you're seeing is because 
of the following line in the snippet:

i['GoogleAnalyticsObject'] = r;

Where `r` is the name of the analytics object.

By using the snippet twice, with the code you've show, you're essentially doing 
this:

i['GoogleAnalyticsObject'] = 'first_namespace_ga';
i['GoogleAnalyticsObject'] = 'second_namespace_ga;

So, once the Google Analytics script downloads, the reference to 
`first_namespace_ga` is completely lost, and the script only transforms the 
second object into a usable function.

If you want to track multiple different properties on the same page, you can do 
that as described here:
https://developers.google.com/analytics/devguides/collection/analyticsjs/advance
d#multipletrackers

I'm going to close this issue since it's not actually a bug. If you have 
additional questions on how to implement multiple trackers, please ask them on 
Stack Overflow using the google-analytics tag or on the official Google group 
for analytics.js:
https://groups.google.com/forum/#!forum/google-analytics-analyticsjs

Original comment by philipwa...@google.com on 6 Aug 2014 at 11:42

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Ok I did not see the race condition on which the GoogleAnalyticsObject is 
overwritten before any analytics library code is loaded.

Too bad the analytics tag did not foresee this use case. Well thanks anyway for 
your answer.

Original comment by laur...@adyoulike.com on 7 Aug 2014 at 12:23