इनलाइन अडैप्टिव बैनर

अडैप्टिव बैनर, रिस्पॉन्सिव विज्ञापनों की अगली-पीढ़ी की टेक्नोलॉजी हैं. ये हर डिवाइस के लिए विज्ञापन के साइज़ को ऑप्टिमाइज़ करके, की परफ़ॉर्मेंस को बेहतर बनाते हैं. अडैप्टिव बैनर, सिर्फ़ तय साइज़ वाले बैनर के साथ काम करते हैं. इन्हें सुधारकर, डेवलपर विज्ञापन की चौड़ाई तय कर सकते हैं. साथ ही, इसका इस्तेमाल करके, विज्ञापन का सही साइज़ तय कर सकते हैं.

इनलाइन अडैप्टिव बैनर विज्ञापनों का सबसे सही साइज़ चुनने के लिए, तय ऊंचाई के बजाय ज़्यादा से ज़्यादा वैल्यू का इस्तेमाल करते हैं. इससे बेहतर परफ़ॉर्मेंस के अवसर मिलते हैं.

इनलाइन अडैप्टिव बैनर का इस्तेमाल कब करना चाहिए

इनलाइन अडैप्टिव बैनर, ऐंकर किए गए अडैप्टिव बैनर की तुलना में, बड़े और लंबे होते हैं. उनकी लंबाई अलग-अलग हो सकती है और उनकी लंबाई डिवाइस की स्क्रीन जितनी हो सकती है.

इन्हें स्क्रोल करने वाले कॉन्टेंट में रखा जाना चाहिए. उदाहरण के लिए:

ज़रूरी शर्तें

शुरू करने से पहले

अपने ऐप्लिकेशन में अडैप्टिव बैनर का इस्तेमाल करते समय, इन बातों का ध्यान रखें:

  • पक्का करें कि आप Google Mobile Ads SDK के सबसे नए वर्शन का इस्तेमाल कर रहे हैं. अगर मीडिएशन का इस्तेमाल कर रहे हैं, तो मीडिएशन अडैप्टर के सबसे नए वर्शन का भी इस्तेमाल कर रहे हैं.

  • इनलाइन अडैप्टिव बैनर साइज़ इस तरह से डिज़ाइन किए गए हैं कि वे उपलब्ध पूरी चौड़ाई का इस्तेमाल करने पर, सबसे अच्छी तरह काम करते हैं. ज़्यादातर मामलों में, यह इस्तेमाल किए जा रहे डिवाइस की स्क्रीन की पूरी चौड़ाई होगी. इसके लिए, सुरक्षित जगहों का ध्यान रखें.

  • विज्ञापन का साइज़ पाने के तरीके

    • AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)
    • AdSize.getLandscapeInlineAdaptiveBannerAdSize(int width)
    • AdSize.getPortraitInlineAdaptiveBannerAdSize(int width)
    • AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
  • इनलाइन अडैप्टिव बैनर एपीआई का इस्तेमाल करते समय, Google Mobile Ads SDK, दी गई चौड़ाई और इनलाइन फ़्लैग के साथ AdSize दिखाता है. इसकी ऊंचाई शून्य या maxHeight है, जो इस पर निर्भर करता है कि आप किस एपीआई का इस्तेमाल कर रहे हैं. विज्ञापन दिखाए जाने पर, उसकी असल ऊंचाई उपलब्ध कराई जाती है.

  • इनलाइन अडैप्टिव बैनर को, स्क्रोल किए जा सकने वाले कॉन्टेंट में दिखाने के लिए डिज़ाइन किया गया है. बैनर डिवाइस की स्क्रीन जितना लंबा हो सकता है या एपीआई के आधार पर, उसकी ऊंचाई ज़्यादा से ज़्यादा तक सीमित हो सकता है.

लागू करने का तरीका

अडैप्टिव बैनर को इनलाइन में आसानी से लागू करने के लिए, नीचे दिया गया तरीका अपनाएं.

  1. अडैप्टिव बैनर विज्ञापन का इनलाइन साइज़ पाएं. आपको स्टैटिक मेथड से साइज़ की जानकारी मिलती है. अडैप्टिव बैनर का अनुरोध करते समय, इस साइज़ का इस्तेमाल किया जाता है. अडैप्टिव विज्ञापन का साइज़ पाने के लिए पक्का करें कि:
    1. सघनता के लिए, डिवाइस की चौड़ाई अलग-अलग पिक्सल में सेट करें. अगर आपको स्क्रीन की पूरी चौड़ाई का इस्तेमाल नहीं करना है, तो अपनी चौड़ाई खुद सेट करें. स्क्रीन की चौड़ाई जानने के लिए, MediaQuery.of(context) का इस्तेमाल किया जा सकता है.
    2. विज्ञापन साइज़ क्लास के लिए सही स्टैटिक तरीकों का इस्तेमाल करें, जैसे कि AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width) मौजूदा ओरिएंटेशन के हिसाब से इनलाइन अडैप्टिव AdSize ऑब्जेक्ट पाने के लिए.
    3. अगर आपको बैनर की ऊंचाई सीमित करनी है, तो स्टैटिक तरीके AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight) का इस्तेमाल करें.
  2. अपने विज्ञापन यूनिट आईडी, ज़रूरत के हिसाब से विज्ञापन का साइज़, और विज्ञापन अनुरोध के ऑब्जेक्ट के साथ BannerAd ऑब्जेक्ट बनाएं.
  3. विज्ञापन लोड करें.
  4. onAdLoaded कॉलबैक में, प्लैटफ़ॉर्म के विज्ञापन का अपडेट किया गया साइज़ पाने और AdWidget कंटेनर की ऊंचाई को अपडेट करने के लिए, BannerAd.getPlatformAdSize() का इस्तेमाल करें.

कोड का उदाहरण

यहां एक विजेट का उदाहरण दिया गया है, जो इनसेट को ध्यान में रखते हुए, स्क्रीन की चौड़ाई में फ़िट होने के लिए इनलाइन अडैप्टिव बैनर लोड करता है:

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

/// This example demonstrates inline adaptive banner ads.
///
/// Loads and shows an inline adaptive banner ad in a scrolling view,
/// and reloads the ad when the orientation changes.
class InlineAdaptiveExample extends StatefulWidget {
  @override
  _InlineAdaptiveExampleState createState() => _InlineAdaptiveExampleState();
}

class _InlineAdaptiveExampleState extends State<InlineAdaptiveExample> {
  static const _insets = 16.0;
  BannerAd? _inlineAdaptiveAd;
  bool _isLoaded = false;
  AdSize? _adSize;
  late Orientation _currentOrientation;

  double get _adWidth => MediaQuery.of(context).size.width - (2 * _insets);

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _currentOrientation = MediaQuery.of(context).orientation;
    _loadAd();
  }

  void _loadAd() async {
    await _inlineAdaptiveAd?.dispose();
    setState(() {
      _inlineAdaptiveAd = null;
      _isLoaded = false;
    });

    // Get an inline adaptive size for the current orientation.
    AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(
        _adWidth.truncate());

    _inlineAdaptiveAd = BannerAd(
      // TODO: replace this test ad unit with your own ad unit.
      adUnitId: 'ca-app-pub-3940256099942544/9214589741',
      size: size,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) async {
          print('Inline adaptive banner loaded: ${ad.responseInfo}');

          // After the ad is loaded, get the platform ad size and use it to
          // update the height of the container. This is necessary because the
          // height can change after the ad is loaded.
          BannerAd bannerAd = (ad as BannerAd);
          final AdSize? size = await bannerAd.getPlatformAdSize();
          if (size == null) {
            print('Error: getPlatformAdSize() returned null for $bannerAd');
            return;
          }

          setState(() {
            _inlineAdaptiveAd = bannerAd;
            _isLoaded = true;
            _adSize = size;
          });
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('Inline adaptive banner failedToLoad: $error');
          ad.dispose();
        },
      ),
    );
    await _inlineAdaptiveAd!.load();
  }

  /// Gets a widget containing the ad, if one is loaded.
  ///
  /// Returns an empty container if no ad is loaded, or the orientation
  /// has changed. Also loads a new ad if the orientation changes.
  Widget _getAdWidget() {
    return OrientationBuilder(
      builder: (context, orientation) {
        if (_currentOrientation == orientation &&
            _inlineAdaptiveAd != null &&
            _isLoaded &&
            _adSize != null) {
          return Align(
              child: Container(
            width: _adWidth,
            height: _adSize!.height.toDouble(),
            child: AdWidget(
              ad: _inlineAdaptiveAd!,
            ),
          ));
        }
        // Reload the ad if the orientation changes.
        if (_currentOrientation != orientation) {
          _currentOrientation = orientation;
          _loadAd();
        }
        return Container();
      },
    );
  }

  @override
  Widget build(BuildContext context) => Scaffold(
      appBar: AppBar(
        title: Text('Inline adaptive banner example'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: _insets),
          child: ListView.separated(
            itemCount: 20,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                height: 40,
              );
            },
            itemBuilder: (BuildContext context, int index) {
              if (index == 10) {
                return _getAdWidget();
              }
              return Text(
                'Placeholder text',
                style: TextStyle(fontSize: 24),
              );
            },
          ),
        ),
      ));

  @override
  void dispose() {
    super.dispose();
    _inlineAdaptiveAd?.dispose();
  }
}