leonardcodep/laravel-adsbygoogle

在您的 Laravel 中显示 Google Adsense 广告。

1.0.2 2021-05-04 05:01 UTC

This package is not auto-updated.

Last update: 2024-10-01 21:43:25 UTC


README

将 Google Adsense 广告包含到您的 Laravel 项目的包。

安装

在项目根目录下运行

composer require leonardcodep/laravel-adsbygoogle

设置配置文件

运行 php artisan vendor:publish --provider="Leonardcodep\Adsbygoogle\AdsbygoogleServiceProvider" --tag="config"

运行(可选)php artisan vendor:publish --provider="Leonardcodep\Adsbygoogle\AdsbygoogleServiceProvider" --tag="views"

编辑在 /config/adsbygoogle.php 中生成的配置文件,以添加您的广告单元

return [
    'client_id' => 'YOUR_CLIENT_ID', //Your Adsense client ID e.g. ca-pub-9508939161510421
    'ads' => [
        'responsive' => [
            'ad_slot' => 12345678901,
            'ad_format' => 'auto',
            'ad_full_width_responsive' => true
        ],
        'rectangle' => [
            'ad_slot' => 5681560223,
            'ad_style' => 'display:inline-block;width:300px;height:250px',
        ],
        'inArticle' => [
            'ad_layout' => 'in-article',
            'ad_format' => 'fluid',
            'ad_slot' => 5149408889,
            'ad_style' => 'display:block; text-align:center;'
        ]
    ]
];

在 Laravel 中注册该包

此包适用于 Laravel 5.5+,因此可以利用自动发现功能,因此不需要手动配置

用法

1°- 将此代码复制并粘贴到您网站的 HTML 中,在 <head> 和 </head> 标签之间。

    @scriptadsbygoogle

显示指令

<script async src="https://#/pagead/js/adsbygoogle.js?client=CLIENT_ID" crossorigin="anonymous"></script>

2°- (可选)将此代码复制并粘贴到您网站的 HTML 中,在 <head> 和 </head> 标签之间。

    @anchoradsbygoogle

显示指令

     <script>
        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "CLIENT_ID",
            enable_page_level_ads: true,
            overlays: {bottom: true}
        });
    </script

3°- 将此代码复制并粘贴到您网站的 HTML 中,在关闭 </body> 标签之前。

    @pushadsbygoogle

显示指令

<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

4°- 要在 blade 模板中显示广告,只需使用 {!! Adsbygoogle::show('responsive') !!},例如。