Difference between revisions of "CentOS 7.x Configure mediawiki to display google adsense advertizements"

From Notes_Wiki
m
m
Line 6: Line 6:


On local machine this usage led to undefined variable type warning.  So commenting entire $wgExtensionCredits[$type][] variable with php /* */ comments seems to have solved the problem.
On local machine this usage led to undefined variable type warning.  So commenting entire $wgExtensionCredits[$type][] variable with php /* */ comments seems to have solved the problem.
==Debug ads not getting displayed issue==
For ads to get displayed the site need to go through:
* Review - https://support.google.com/adsense/thread/24997874?hl=en
* Then ads.txt for approved site needs to be placed in document_root
* Then auto ads need to be enable for site using google.com/adsense
Each of these steps might take a few days for google to complete from their side.  Once all three steps are done, the ads should get displayed and there might be corresponding revenue also.





Revision as of 12:28, 14 January 2020

<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>

CentOS 7.x Configure mediawiki to display google adsense advertizements

Enable auto-ads using HeadScript extension

Google has created auto-ads which require putting only one JS script between head and /head elements. That can be done via Headscript extension ( https://www.mediawiki.org/wiki/Extension:HeadScript ) which is very straightforward (Single PHP file) to setup and use.

On local machine this usage led to undefined variable type warning. So commenting entire $wgExtensionCredits[$type][] variable with php /* */ comments seems to have solved the problem.


Debug ads not getting displayed issue

For ads to get displayed the site need to go through:

Each of these steps might take a few days for google to complete from their side. Once all three steps are done, the ads should get displayed and there might be corresponding revenue also.


Display fixed adds in header and footer using site hooks

  1. Create Google Adsense Account
  2. Optionally, Link adsense account to youtube channel
  3. Create appropriate ad unit for displaying ads on mediawiki and get script code.
  4. Paste script code in Localsettings.php file to display google adsense in mediawiki


Header advertizement script

$wgHooks['SiteNoticeAfter'][] = function(&$siteNotice, $skin) {
        $siteNotice .= <<< EOT
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XXX -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxx"
     data-ad-slot="xxxxxx"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOT;
        return true;
};

Remember to replace script in above code with script available in your google adsense account related to the ad

Footer advertizement script

$wgHooks['SkinAfterContent'][] = function(&$data, $skin) {
        global $myAdCode;
        $data .= '<div style="text-align:center;">';
        $data .= <<< EOT
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XXX -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxx"
     data-ad-slot="xxxxx"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOT;
 
        $data .= '</div>';
        return true;
};

Remember to replace script in above code with script available in your google adsense account related to the ad


Refer:


Setup mediawiki adsense extension

Latest extension updated for mediawiki 1.31+. However due to missing extension.json file it does not works

  1. Create account on www.google.com/adsense It might take 1 or 2 days for account to be activated
  2. After activation go to Ads -> Ad units and create a new "Ad unit". Copy the script code displayed at end for valuable parameters required later.
  3. Download mediawiki Adsense extension from https://www.mediawiki.org/wiki/Extension:Google_AdSense
  4. Extract downloaded extension files in ./extensions/ folder inside mediawiki source base folder
  5. Add following to localsettings.php
    require_once "$IP/extensions/GoogleAdSense/GoogleAdSense.php";
    $wgGoogleAdSenseClient = 'none'; // Client ID for your AdSense script (example: ca-pub-1234546403419693)
    $wgGoogleAdSenseSlot = 'none'; // Slot ID for your AdSense script (example: 1234580893)
    $wgGoogleAdSenseID = 'none'; //Name of ad unit created above. This would be part of Adsense script comment
    $wgGoogleAdSenseSrc = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; //This is part of script
  6. Reload mediawiki and test. There should be "Advertizements" under tools on the left side.


<yambe:breadcrumb self="Configure mediawiki to display google adsense advertizements">CentOS_7.x_Mediawiki|Mediawiki</yambe:breadcrumb>