yaroslawww / laravel-ad-director
帮助您在网站上添加广告的包。
1.1.0
2023-08-06 11:01 UTC
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.1
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.11
This package is auto-updated.
Last update: 2024-09-06 13:24:49 UTC
README
帮助您在网站上添加广告的包。
安装
使用composer安装包
composer require yaroslawww/laravel-ad-director
可选地,您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="AdDirector\ServiceProvider" --tag="config"
支持的服务
- Google Publisher Tags
- 要添加新服务,请发送PR。
用法
如果您使用Google Publisher Tags,并使用重复的大小 - 您可以在任何服务提供者中轻松配置全局大小映射,因为GPT是单例。
use AdDirector\Facades\AdDirector; use AdDirector\GPT\SizeMap; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { AdDirector::gpt() // Add size with mapping example ::addSize('leaderboard', new \AdDirector\GPT\Size( // Allowed sizes [[728, 90], [320, 50]], // Responsive size map (new SizeMap()) // Tablet ->addSize([1024, 768], [728, 90]) ->addSize([980, 690], [728, 90]) // Desktop ->addSize([1050, 200], [728, 90]) // Mobile ->addSize([640, 480], [320, 50]) // Any size ->addSize([0, 0], [320, 50]) )) // Add static size without responsive ::addSize('medium_rectangle', new \AdDirector\GPT\Size([300, 250])); } }
在页面控制器中,您可以配置广告位置。
use AdDirector\Facades\AdDirector; use AdDirector\GPT\Slot; class FrontpageController extends Controller { public function __invoke() { AdDirector::gpt() ->addLocation(Slot::make( '/1234567/FOO_Leaderboard', // Size name from global config 'leaderboard', // Optional, if not set will be generated automatically 'div-gpt-ad-1234567890001-0' // Location identifier, if not set will be used slot's adUnitPath ) // Set targeting ->addTarget('foo', 'bar') ->addTarget('baz', ['foo', 'bar']) , 'header-ads') ->addLocation(Slot::make( '/1234567/FOO_Medium', 'medium_rectangle', ), 'medium-ads') ->addLocation(Slot::make( '/1234567/BAR_Leaderboard', // Manually set custom size new \AdDirector\GPT\Size( [[728, 90], [320, 50]], (new SizeMap()) ->addSize([1024, 768], [728, 90]) ->addSize([640, 480], [320, 50]) ->addSize([0, 0], [320, 50]) ), ), 'footer-ads'); return view('frontpage'); } }
现在是向模板添加脚本的时候了
<head> {!! \AdDirector\Facades\AdDirector::configurationScript() !!} </head> <body> <div id="header"> {!! \AdDirector\Facades\AdDirector::adLocation('header-ads') !!} </div> <div id="content"> <div>Content</div> <div> {!! \AdDirector\Facades\AdDirector::adLocation('medium-ads-ads') !!} </div> <div>Content</div> </div> <div id="footer"> {!! \AdDirector\Facades\AdDirector::adLocation('footer-ads') !!} </div> <script> {{-- There possible to add any js confitions, timeout, etc.. --}} {!! \AdDirector\Facades\AdDirector::displayScript() !!} </script> </body> </html>