项目作者: IsAmrish

项目描述 :
A Gatsby plugin to easily add a google adsense to your gatsby site
高级语言: JavaScript
项目地址: git://github.com/IsAmrish/gatsby-plugin-google-adsense.git
创建时间: 2019-08-04T06:43:27Z
项目社区:https://github.com/IsAmrish/gatsby-plugin-google-adsense

开源协议:MIT License

下载



gatsby-plugin-google-adsense


Add Google Adsense to your Gatsby site.


npm (scoped)
npm

Install

  1. npm install @isamrish/gatsby-plugin-google-adsense

or

  1. yarn add @isamrish/gatsby-plugin-google-adsense

Usage in Gatsby website

  1. // In your gatsby-config.js
  2. module.exports = {
  3. plugins: [
  4. {
  5. resolve: `@isamrish/gatsby-plugin-google-adsense`,
  6. options: {
  7. googleAdClientId: "YOUR_GOOGLE_ADSENSE_TRACKING_ID",
  8. head: false // Optional
  9. }
  10. }
  11. ]
  12. };

Options

googleAdClientId

Here you place your Google Adsense tracking id.

head

Here you can define where to place the tracking script. With head:true it will placed in the header, with head:false it will placed in the body. Default is false.

Google adsense recommends to put script in head tag.

Example of Adsense component

  1. // In your adsense component
  2. import React from "react";
  3. export default function AdSense() {
  4. React.useEffect(() => {
  5. if (process.env.NODE_ENV !== "development") {
  6. if (window) {
  7. try {
  8. (window.adsbygoogle = window.adsbygoogle || []).push({});
  9. } catch (error) {
  10. console.log(error, "adsenese error");
  11. }
  12. }
  13. }
  14. }, []);
  15. return (
  16. <ins
  17. className="adsbygoogle"
  18. data-ad-client="ca-pub-XXXXXX"
  19. data-ad-slot="XXXXXXX"
  20. style={{
  21. display: "block"
  22. }}
  23. data-ad-format="auto"
  24. data-full-width-responsive="true"
  25. ></ins>
  26. );
  27. }