Introduction
A comprehensive Django application for managing and displaying Yandex advertising blocks with advanced configuration options.
Already built-in features:
- Multiple Ad Types: Support for banners, full-screen ads, floor ads, top ads, carousels, and in-image ads
- Platform Targeting: Display ads on specific platforms (desktop, mobile, or cross-platform)
- Flexible Configuration: Manage ad locations, display frequency, and placement rules
- Middleware Integration: Automatic ad injection into templates based on view and template rules
- Customizable Templates: Extensible template system for different ad formats
- Context-Aware: Smart ad placement with context data integration
- Pagination Support: Automatic ad insertion between content pages
Models Overview
This app operates using four models: models for Ad Blocks (YandexAdBlock), models for the location of these ad blocks (YandexAdLocation), models for the blocks' configuration (YandexAdBlockConfiguration, which determines which units to use), and models for the current configuration (YandexCurrentAdBlockConfiguration, which determines which configuration to use on the website).
Let's look at each model separately.
YandexAdBlock
Defines individual ad blocks with properties:
- Ad Types:
- banner — adapts to the size of the surrounding container. The same block can be placed simultaneously on different versions of the website, and it will be shown on all devices.
- fullscreen — displays on the entire screen. A separate block must be created for each version of the website.
- floorAd — is fixed at the bottom of the screen over the website content. A separate block must be created for each version of the website.
- topAd — is fixed at the top of the screen over the website content on mobile devices only.
- feed — is displayed as a feed of ads. The same block can be placed simultaneously on different versions of the website, and it will be shown on all devices.
- inImage — is displayed over images on the website. The same block can be placed simultaneously on different versions of the website, and it will be shown on all devices.
- Push Triggers(A moment to load an Ad)
- on-load means as soon as possible
- on-intersection means when user's viewport intersects with an Ad element.
- Platform Targeting:
- Desktop — only on desktop devices
- Mobile — only on mobile devices
- Cross-platform — on both
Also, to connect the newly created record with the block made in YAN. You need to copy and paste the provided ID like this: R-A-17293858-16.
YandexAdLocation
Maps an Ad'sad's blocks to specific locations on your site. You should specify them by yourself. For example, you decide to display your ad on the home page and insert this:
When creating a new record in the database, you should name your location by HOME_FIRST_VISIBLE and choose the block to display.
YandexAdBlockConfiguration
Groups ad locations and defines display frequency for pagination pages, if there are some.
YandexCurrentAdBlockConfiguration
Singleton model to manage the currently active ad configuration.
Installation
This Django application is available on PyPI and can be downloaded using this command:
After installing the application, you will need to connect it and configure it for correct operation. Add it to INSTALLED_APPS and MIDDLEWARE.
Since this is middleware, it is necessary to first indicate for which templates(and views) it is worthwhile to carry out work on determining advertising locations. You need to specify two arrays:
Done. Everything is set up and configured. The last thing to do is to apply migrations and collect some static files.
Now we've done for sure. The next thing left to do is to prepare your Django-templates to be used by YandexAdManager middleware.
Configuring templates
Firstly, we need to include header template. Include these in your base template's head section (
<head> tag):Then, include scripts and styles:
Setting up ad locations
The last thing left to do is decide where we want to display the ad. To do this, select the location where you'd like to display the ad and insert the following code:
Single Ad location
When you create another entry in the database through the admin panel, insert into the field with the location name — AD_LOCATION_NAME.
Multiple Ad locations
If a single page contains multiple ad locations using the same ad unit, you'll need to use the unificator variable to ensure proper operation with Yandex's API and avoid confusion.
Or like this:
Ad location in pagination
If you want ads to appear on pagination pages between articles, for example, you should use this template:
Best Practices
- Use descriptive unificators to track ad performance in different locations
- Test on multiple devices when using platform targeting
- Monitor ad density to avoid overwhelming users
- Use intersection-based loading for better performance
- Configure appropriate ad types for different content contexts
Troubleshooting
Ads Not Showing
- Check if middleware is properly configured
- Verify
isYandexAdManagerMiddlewareConnectedis True in templates - Ensure ad locations are properly configured in admin
- Check browser console for JavaScript errors
Note: This package is not officially affiliated with Yandex. It's a community-maintained Django integration for Yandex Advertising Network.
Reviews
(0)