yaroslawww / laravel-ad-director
Package to help you add advertising to your site.
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-11-06 13:43:00 UTC
README
Package to help you add advertising to your site.
Installation
Install the package via composer:
composer require yaroslawww/laravel-ad-director
Optionally you can publish the config file with:
php artisan vendor:publish --provider="AdDirector\ServiceProvider" --tag="config"
Supported services
- Google Publisher Tags
- to add new services, please send PR.
Usage
If you use Google Publisher Tags, and use repeated sizes - you can easily configure global sizes with size mapping in any service provider - as GPT is singleton.
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])); } }
On page controller you can configure set of advert locations.
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'); } }
Now there time to add scripts to template
<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>