Difference between revisions of "Adding Google Analytics and AdSense to MediaWiki"

From GlassTTY
Jump to: navigation, search
m (Auto Ads)
m (John moved page Adding Google Analytics to MediaWiki to Adding Google Analytics and AdSense to MediaWiki: Adding Google Analytics and AdSense to MediaWiki)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Google Analytics==
 
==Google Analytics==
  
Add the following to the Common.js file. This can be done via the web interface via the following link
+
Add the following to the bottom of the LocalSettings.php file, replacing ''TRACKNGID'' with an appropriate Google tracking ID.
  
     /index.php/MediaWiki:Common.js
+
     $wgHooks['BeforePageDisplay'][] = function( OutputPage &$out, Skin &$skin ) {
 
+
        $code = <<<HTML
Add the google analytics code   
+
     <!-- Global site tag (gtag.js) - Google Analytics -->
 
+
    <script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+
    <script>
     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+
      window.dataLayer = window.dataLayer || [];
     m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+
      function gtag(){dataLayer.push(arguments);}
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
      gtag('js', new Date());
 +
      
 +
      gtag('config', 'TRACKINGID');
 +
    </script>
 +
    HTML;
 
      
 
      
    ga('create', 'UA-109426764-1', 'auto');
+
    $out->addHeadItem( 'gtag-insert', $code );
    ga('send', 'pageview');
+
    return true;
 
+
};
Naturally substitute your own GA code for the ''UA-109426764-1'' specified above.
 
 
 
==Google Auto Ads==
 
  
AutoAds once enabled in Google AdSense make use of the following code.
+
==Google AdSense==
  
    <script data-ad-client="ca-pub-3637352270357717" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
+
Add the appropriate AdSense code to the bottom of the LocalSettings.php file e.g.
  
Naturally substitute your own GA code for the ''UA-109426764-1'' specified above.
+
    $wgHooks['SkinAfterContent'][] = function(&$siteNotice, $skin) {
 +
            $siteNotice .= <<< EOT
 +
    <p>&nbsp;</p>
 +
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
 +
    <!-- Wiki Page Ad -->
 +
    <ins class="adsbygoogle"
 +
        style="display:block"
 +
        data-ad-client="ca-pub-3637352270357717"
 +
        data-ad-slot="9055233069"
 +
        data-ad-format="auto"></ins>
 +
    <script>
 +
    (adsbygoogle = window.adsbygoogle || []).push({});
 +
    </script>
 +
    <p>&nbsp;</p>
 +
    EOT;
 +
    return true;
 +
};

Latest revision as of 07:14, 2 December 2019

Google Analytics

Add the following to the bottom of the LocalSettings.php file, replacing TRACKNGID with an appropriate Google tracking ID.

   $wgHooks['BeforePageDisplay'][] = function( OutputPage &$out, Skin &$skin ) {
        $code = <<<HTML
   <script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
   <script>
     window.dataLayer = window.dataLayer || [];
     function gtag(){dataLayer.push(arguments);}
     gtag('js', new Date());
   
     gtag('config', 'TRACKINGID');
   </script>
   HTML;
   
    $out->addHeadItem( 'gtag-insert', $code );
    return true;

};

Google AdSense

Add the appropriate AdSense code to the bottom of the LocalSettings.php file e.g.

   $wgHooks['SkinAfterContent'][] = function(&$siteNotice, $skin) {
           $siteNotice .= <<< EOT

 

   <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
   
   <script>
   (adsbygoogle = window.adsbygoogle || []).push({});
   </script>

 

   EOT;
   return true;

};