Google analytics inside shadow DOM doesn't work

泪湿孤枕 提交于 2019-12-11 08:36:40

问题


In my chrome extension instead of using any html pages, shadow DOM is being added from the background js pages.

It seems like no events are being pushed for analytics.

After adding the extension as shadow root the structure looks as follows

Now the chrome extension manifest.json looks like

"manifest_version": 2,
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",

"background": {
"scripts": ["js/background.js"],
"persistent": true
},

and few other background files are loaded.

In background.js the main file where all the DOM elements added is loaded as follows

var mainJS = chrome.extension.getURL("js/chromewindow.js");
            var rawFile = new XMLHttpRequest();
            rawFile.open("GET", mainJS, true);
            rawFile.onreadystatechange = function() {
                if (rawFile.readyState === 4 && (rawFile.status === 200 || rawFile.status === 0)) {
                    var text = rawFile.responseText;
                    chrome.tabs.executeScript(tab.id, {
                        code: text + ";onMyExtensionLoad('" + from + "');"
                    });
                }
            };

In chromewindow.js I added the usual analytics code as follows

var _gaq = _gaq || [];

var ga = document.createElement('script'); 
ga.type = 'text/javascript'; 
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0]; 
s.parentNode.insertBefore(ga, s);

_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

I also include the following code to track the onclick events

function trackButton(e) 
{
    _gaq.push(['_trackEvent', e.target.id, 'clicked']);
    console.log("Track event called");

};

When I execute this I could find that the ga_debug.js file being loaded. However I could not find any request like ___utm.gif as recommended.

There are no errors in the console too.

I wounder where I'm missing things. Kindly help.

来源:https://stackoverflow.com/questions/40544304/google-analytics-inside-shadow-dom-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!