@carlos.algms/react-gpt

中文文档
2.2.2发布于 2024/10/11MITCommonJS近 30 天下载 1.3万

A react display ad component using Google Publisher Tag

原始使用文档
文档目录9 节
  1. Requirements
  2. Browser Requirements
  3. Features
  4. Installation
  5. Getting Started
  6. API and Documentation
  7. To run examples:
  8. Contributing to this project
  9. License

React GPT

npm Version Build Status Dependency Status codecov.io PRs Welcome

A React component for Google Publisher Tags.

Requirements

  • React 0.14+

Browser Requirements

  • IE10+

Features

  • Supports all rendering modes (single request mode, async rendering node and *sync rendering mode)
  • Supports responsive ads.
  • Supports interstitial ads.
  • Supports lazy render.

* Synchronous rendering requires that the GPT JavaScript be loaded synchronously.

Installation

$ yarn react-gpt

React GPT depends on Promise to be available in browser. If your application support the browser which doesn't support Promise, please include the polyfill.

Getting Started

Import React GPT and pass props to the component.

import {Bling as GPT} from "react-gpt";

class Application extends React.Component {
    render() {
        return (
            <GPT
                adUnitPath="/4595/nfl.test.open"
                slotSize={[728, 90]}
            />
        );
    }
}

You at least need to pass adUnitPath and one of slotSize and sizeMapping.

Enabling Single Request Mode

To enable Single Request Mode, call Bling.enableSingleRequest() before rendering any ad. It defaults to Asynchronous Rendering Mode if not set.

import {Bling as GPT} from "react-gpt";

GPT.enableSingleRequest();

class Application extends React.Component {
    render() {
        return (
            <div id="ad-1">
                <GPT
                    adUnitPath="/4595/nfl.test.open"
                    slotSize={[728, 90]}
                />
            </div>
            <div id="ad-2">
                <GPT
                    adUnitPath="/4595/nfl.test.open"
                    slotSize={[300, 250]}
                />
            </div>
        );
    }
}

The above example will make one request to the server to render both ads which makes it easier to ensure category exclusion.

Responsive ad

If you pass sizeMapping props instead of slotSize, React GPT listens for the viewport width change and refreshes an ad when the break point is hit.

import {Bling as GPT} from "react-gpt";

class Application extends React.Component {
    render() {
        return (
            <GPT
                adUnitPath="/4595/nfl.test.open"
                sizeMapping={[
                    {viewport: [0, 0], slot: [320, 50]},
                    {viewport: [750, 0], slot: [728, 90]},
                    {viewport: [1050, 0], slot: [1024, 120]}
                ]}
            />
        );
    }
}

API and Documentation

To run examples:

  1. Clone this repo
  2. Run yarn
  3. Run npm run examples for client side rendering, npm start for server side rendering.
  4. Point your browser to http://localhost:8080

Contributing to this project

Please take a moment to review the guidelines for contributing.

License

MIT

版本与发布记录

@carlos.algms/react-gpt 共发布 5 个版本,首次发布于 2019/04/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.2latest2024/10/112 年前
2.2.12021/12/014 年前
2.2.02021/10/215 年前
2.1.02021/05/075 年前
2.0.22019/04/057 年前

关于 @carlos.algms/react-gpt 的常见问题

@carlos.algms/react-gpt 是什么?

A react display ad component using Google Publisher Tag

如何安装 @carlos.algms/react-gpt?

在项目目录运行 npm install @carlos.algms/react-gpt,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:prop-types(^15.5.10)、react(^15.0.1 || ^16.0.0)、react-dom(^15.0.1 || ^16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@carlos.algms/react-gpt 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/carlos.algms__react-gpt 是否存在。

@carlos.algms/react-gpt 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@carlos.algms/react-gpt 可以商用吗?

@carlos.algms/react-gpt 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carlos.algms/react-gpt 的下载量有多少?

根据 npm 官方下载统计,@carlos.algms/react-gpt 在 2026/10/09 – 2026/11/07 这 30 天内被下载 13,036 次,日均约 435 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carlos.algms/react-gpt 还在维护吗?

最新版本 2.2.2 发布于 2024/10/11(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。