简体   繁体   English

是否可以将Google Analytics(分析)添加到没有popup.html的Chrome扩展程序中?

[英]Can I add Google Analytics to a Chrome extension which doesn't have a popup.html?

My chrome extension is recognising a specific tab's URL, and then, on icon click, it modifies that active tab's content. 我的chrome扩展程序识别出特定标签的URL,然后在单击图标时修改该活动标签的内容。 I would like to gauge the usage of it, in some way. 我想以某种方式来评估它的用法。 This is the extension's logic: 这是扩展程序的逻辑:

- manifest.json [injecting jquery & jquery plugin]
- background.js [ on extension's icon click, it executes content_script.js and tracking.js]
- icon
- content_script.js [ it modifies page content ]
- tracking.js (GA code, as provided by google Analytics then modified according to https://davidsimpson.me/2014/05/27/add-googles-universal-analytics-tracking-chrome-extension/ );

I would like to see how many times extension's button is clicked. 我想查看扩展按钮被点击了多少次。 I tried adding the appropriate permissions to manifest.json: 我尝试将适当的权限添加到manifest.json:

  "content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"

and adding Analytics code to a separate .js file (tracking.js), and executing it when the extension's icon is clicked, as specified in background.js below: 并将Analytics(分析)代码添加到单独的.js文件(tracking.js),然后在单击扩展程序的图标时执行该代码,如以下background.js中所指定:

chrome.browserAction.onCLicked.addListener(
function(tab) {
chrome.tabs.executeScript(null, {file:
"content_script.js"});
chrome.tabs.executeScript(null, {file:
"tracking.js"});
});

.. it doesn't work, in the sense that Google Analytics is telling me 'tracking not installed', and I cannot think of another way of adding it. ..从Google Analytics(分析)告诉我“未安装跟踪”的意义上来说,它是行不通的,我想不出另一种添加它的方法。

How can I see this extension's usage? 如何查看此扩展程序的用法?

What you are trying to do is to inject tracking into a tab as a content script. 您要做的是将跟踪作为内容脚本注入到选项卡中。

Tracking works by adding another <script> tag into the page, which kicks it out of your content script's isolated context and into the page's context. 通过在页面中添加另一个<script>标记来进行跟踪,从而将其从内容脚本的隔离上下文中踢出并进入页面上下文。 That's why your script can't see the tracking enabled (and you possibly just broke the page's own tracking too). 这就是为什么您的脚本无法看到启用的跟踪的原因(您也可能刚刚破坏了页面的跟踪)。

You need to install the tracking code in your background page and send events from there. 您需要在后台页面中安装跟踪代码,然后从那里发送事件。 To do so, simply include tracking.js to the list of background scripts; 为此,只需将tracking.js包括在后台脚本列表中即可; after that, send events in the usual GA way. 之后,以常规GA方法发送事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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