AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Friday 4 March 2016

How to add google analytics code in chrome extension?

In this article I will discuss How to add google analytics code in google chrome extension? Google analytic is use to check your site analytic & performance.

Step 1: Add following code in manifest.json file.

{     
 some other code.......
 "content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"
}

Step 2: Create new javascript file "analytics.js" and write below code

  (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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'your-analytics-is', 'auto'); // Write your analytics id here
  ga('set', 'checkProtocolTask', function(){}); 
  ga('require', 'displayfeatures');
  ga('send', 'pageview', '/CO-Extension.html');

Step 3: Call that file in extension HTML file.
?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    .......some other scripts
    <script type="text/javascript" src="js/analytics.js"></script>   
</head>


0 comments :

Post a Comment

  • Popular Posts
  • Comments