Skip to content

Commit 5437423

Browse files
committed
Remove stubs and expose AdUnitWidget directly.
1 parent 54fd0c5 commit 5437423

16 files changed

+127
-145
lines changed

packages/google_adsense/CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## 0.0.2
22

33
* **Breaking changes**: Reshuffles API exports:
4-
* Renames `experimental/google_adsense` to `experimental/adsense`.
5-
* Removes `AdStatus` and `AdUnitParams` from `experimental/google_adsense`.
6-
* Moves `AdSense` and its `adSense` singleton to the root export.
7-
* Removes the `adUnit` method from `AdSense`, which is now provided through
8-
an extension from `experimental/adsense`.
4+
* Removes the `adUnit` method, and instead exports the `AdUnitWidget` directly.
5+
* Renames `experimental/google_adsense` to `experimental/ad_unit_widget.dart`.
6+
* Removes the `AdStatus` and `AdUnitParams` exports.
7+
* Removes the "stub" files, so this package is now web-only and must be used
8+
through a conditional import.
99
* Tweaks several documentation pages to remove references to internal APIs.
1010
* Splits tests to reflect the new code structure.
1111

packages/google_adsense/README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To start displaying ads, initialize AdSense with your [Publisher ID](https://sup
1818

1919
<?code-excerpt "example/lib/main.dart (init)"?>
2020
```dart
21-
import 'package:google_adsense/experimental/adsense.dart';
21+
import 'package:google_adsense/experimental/ad_unit_widget.dart';
2222
import 'package:google_adsense/google_adsense.dart';
2323
2424
void main() {
@@ -30,13 +30,22 @@ void main() {
3030
}
3131
```
3232

33-
### Enable Auto Ads
34-
In order to start displaying [Auto ads](https://support.google.com/adsense/answer/9261805) make sure to configure this feature in your AdSense Console. If you want to display ad units within your app content, continue to the next step
33+
### Displaying Auto Ads
34+
In order to start displaying [Auto ads](https://support.google.com/adsense/answer/9261805):
3535

36-
### Display ad unit Widget
36+
1. Configure this feature in your AdSense Console.
3737

38-
1. Create [ad units](https://support.google.com/adsense/answer/9183549) in your AdSense account
39-
2. Use relevant `AdUnitConfiguration` constructor as per table below
38+
Auto ads should start showing just with the call to `initialize`, when available.
39+
40+
If you want to display ad units within your app content, continue to the next step
41+
42+
### Display ad units (`AdUnitWidget`)
43+
44+
To display an Ad unit in your Flutter application:
45+
46+
1. Create [ad units](https://support.google.com/adsense/answer/9183549) in your AdSense account.
47+
This will provide an HTML snippet, which you need to translate to Dart.
48+
2. Pick the `AdUnitConfiguration` for your ad type:
4049

4150
| Ad Unit Type | `AdUnitConfiguration` constructor method |
4251
|----------------|--------------------------------------------|
@@ -81,14 +90,18 @@ and:
8190

8291
<?code-excerpt "example/lib/main.dart (adUnit)"?>
8392
```dart
84-
adSense.adUnit(AdUnitConfiguration.displayAdUnit(
85-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
86-
adFormat: AdFormat
87-
.AUTO, // Remove AdFormat to make ads limited by height
88-
))
93+
AdUnitWidget(
94+
configuration: AdUnitConfiguration.displayAdUnit(
95+
// TODO: Replace with your Ad Unit ID
96+
adSlot: '1234567890',
97+
// Remove AdFormat to make ads limited by height
98+
adFormat: AdFormat.AUTO,
99+
),
100+
),
89101
```
90102

91-
#### Customize ad unit Widget
103+
#### `AdUnitWidget` customizations
104+
92105
To [modify your responsive ad code](https://support.google.com/adsense/answer/9183363?hl=en&ref_topic=9183242&sjid=11551379421978541034-EU):
93106
1. Make sure to follow [AdSense policies](https://support.google.com/adsense/answer/1346295?hl=en&sjid=18331098933308334645-EU&visit_id=638689380593964621-4184295127&ref_topic=1271508&rd=1)
94107
2. Use Flutter instruments for [adaptive and responsive design](https://docs.flutter.dev/ui/adaptive-responsive)
@@ -102,10 +115,14 @@ Container(
102115
constraints:
103116
const BoxConstraints(maxHeight: 100, maxWidth: 1200),
104117
padding: const EdgeInsets.only(bottom: 10),
105-
child: adSense.adUnit(AdUnitConfiguration.displayAdUnit(
106-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
107-
// adFormat: AdFormat.AUTO, // Not using AdFormat to make ad unit respect height constraint
108-
)),
118+
child: AdUnitWidget(
119+
configuration: AdUnitConfiguration.displayAdUnit(
120+
// TODO: Replace with your Ad Unit ID
121+
adSlot: '1234567890',
122+
// Do not use adFormat to make ad unit respect height constraint
123+
// adFormat: AdFormat.AUTO,
124+
),
125+
),
109126
),
110127
```
111128
## Testing and common errors
@@ -126,6 +143,7 @@ Make sure to set correct values to adSlot and adClient arguments
126143
### Ad unfilled
127144

128145
There is no deterministic way to make sure your ads are 100% filled even when testing. Some of the way to increase the fill rate:
146+
- Ensure your ad units are correctly configured in AdSense
129147
- Try setting `adTest` parameter to `true`
130148
- Try setting AD_FORMAT to `auto` (default setting)
131149
- Try setting FULL_WIDTH_RESPONSIVE to `true` (default setting)

packages/google_adsense/example/integration_test/experimental_google_adsense_test.dart renamed to packages/google_adsense/example/integration_test/experimental_ad_unit_widget_test.dart

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import 'package:flutter/material.dart';
1010
import 'package:flutter_test/flutter_test.dart';
11-
import 'package:google_adsense/experimental/adsense.dart';
11+
import 'package:google_adsense/experimental/ad_unit_widget.dart';
1212
// Ensure we don't use the `adSense` singleton for tests.
1313
import 'package:google_adsense/google_adsense.dart' hide adSense;
1414
import 'package:google_adsense/src/adsense/ad_unit_params.dart';
15-
import 'package:google_adsense/src/adsense/ad_unit_widget.dart';
1615
import 'package:integration_test/integration_test.dart';
1716

1817
import 'adsense_test_js_interop.dart';
@@ -48,11 +47,12 @@ void main() async {
4847

4948
adSense.initialize(testClient);
5049

51-
final Widget adUnitWidget = adSense.adUnit(
52-
AdUnitConfiguration.displayAdUnit(
50+
final Widget adUnitWidget = AdUnitWidget(
51+
configuration: AdUnitConfiguration.displayAdUnit(
5352
adSlot: testSlot,
5453
adFormat: AdFormat.AUTO, // Important!
5554
),
55+
adClient: adSense.adClient,
5656
);
5757

5858
await pumpAdWidget(adUnitWidget, tester);
@@ -81,10 +81,11 @@ void main() async {
8181

8282
adSense.initialize(testClient);
8383

84-
final Widget adUnitWidget = adSense.adUnit(
85-
AdUnitConfiguration.displayAdUnit(
84+
final Widget adUnitWidget = AdUnitWidget(
85+
configuration: AdUnitConfiguration.displayAdUnit(
8686
adSlot: testSlot,
8787
),
88+
adClient: adSense.adClient,
8889
);
8990

9091
final Widget constrainedAd = Container(
@@ -109,10 +110,11 @@ void main() async {
109110
mockAdsByGoogle(mockAd(adStatus: AdStatus.UNFILLED));
110111

111112
adSense.initialize(testClient);
112-
final Widget adUnitWidget = adSense.adUnit(
113-
AdUnitConfiguration.displayAdUnit(
113+
final Widget adUnitWidget = AdUnitWidget(
114+
configuration: AdUnitConfiguration.displayAdUnit(
114115
adSlot: testSlot,
115116
),
117+
adClient: adSense.adClient,
116118
);
117119

118120
await pumpAdWidget(adUnitWidget, tester);
@@ -142,19 +144,28 @@ void main() async {
142144

143145
final Widget bunchOfAds = Column(
144146
children: <Widget>[
145-
adSense.adUnit(AdUnitConfiguration.displayAdUnit(
146-
adSlot: testSlot,
147-
adFormat: AdFormat.AUTO,
148-
)),
149-
adSense.adUnit(AdUnitConfiguration.displayAdUnit(
150-
adSlot: testSlot,
151-
adFormat: AdFormat.AUTO,
152-
)),
147+
AdUnitWidget(
148+
configuration: AdUnitConfiguration.displayAdUnit(
149+
adSlot: testSlot,
150+
adFormat: AdFormat.AUTO,
151+
),
152+
adClient: adSense.adClient,
153+
),
154+
AdUnitWidget(
155+
configuration: AdUnitConfiguration.displayAdUnit(
156+
adSlot: testSlot,
157+
adFormat: AdFormat.AUTO,
158+
),
159+
adClient: adSense.adClient,
160+
),
153161
Container(
154162
constraints: const BoxConstraints(maxHeight: 100),
155-
child: adSense.adUnit(AdUnitConfiguration.displayAdUnit(
156-
adSlot: testSlot,
157-
)),
163+
child: AdUnitWidget(
164+
configuration: AdUnitConfiguration.displayAdUnit(
165+
adSlot: testSlot,
166+
),
167+
adClient: adSense.adClient,
168+
),
158169
),
159170
],
160171
);

packages/google_adsense/example/lib/main.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import 'package:flutter/material.dart';
88

99
// #docregion init
10-
import 'package:google_adsense/experimental/adsense.dart';
10+
import 'package:google_adsense/experimental/ad_unit_widget.dart';
1111
import 'package:google_adsense/google_adsense.dart';
1212

1313
void main() {
@@ -69,13 +69,15 @@ class _MyHomePageState extends State<MyHomePage> {
6969
padding: const EdgeInsets.only(bottom: 10),
7070
child:
7171
// #docregion adUnit
72-
adSense.adUnit(AdUnitConfiguration.displayAdUnit(
73-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
74-
adFormat: AdFormat
75-
.AUTO, // Remove AdFormat to make ads limited by height
76-
))
72+
AdUnitWidget(
73+
configuration: AdUnitConfiguration.displayAdUnit(
74+
// TODO: Replace with your Ad Unit ID
75+
adSlot: '1234567890',
76+
// Remove AdFormat to make ads limited by height
77+
adFormat: AdFormat.AUTO,
78+
),
79+
),
7780
// #enddocregion adUnit
78-
,
7981
),
8082
const Text(
8183
'Responsive Ad Constrained by height of 100px and width of 1200px (to keep ad centered):',
@@ -85,10 +87,14 @@ class _MyHomePageState extends State<MyHomePage> {
8587
constraints:
8688
const BoxConstraints(maxHeight: 100, maxWidth: 1200),
8789
padding: const EdgeInsets.only(bottom: 10),
88-
child: adSense.adUnit(AdUnitConfiguration.displayAdUnit(
89-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
90-
// adFormat: AdFormat.AUTO, // Not using AdFormat to make ad unit respect height constraint
91-
)),
90+
child: AdUnitWidget(
91+
configuration: AdUnitConfiguration.displayAdUnit(
92+
// TODO: Replace with your Ad Unit ID
93+
adSlot: '1234567890',
94+
// Do not use adFormat to make ad unit respect height constraint
95+
// adFormat: AdFormat.AUTO,
96+
),
97+
),
9298
),
9399
// #enddocregion constraints
94100
const Text(
@@ -98,10 +104,13 @@ class _MyHomePageState extends State<MyHomePage> {
98104
height: 125,
99105
width: 125,
100106
padding: const EdgeInsets.only(bottom: 10),
101-
child: adSense.adUnit(AdUnitConfiguration.displayAdUnit(
102-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
103-
// adFormat: AdFormat.AUTO, // Not using AdFormat to make ad unit respect height constraint
104-
isFullWidthResponsive: false)),
107+
child: AdUnitWidget(
108+
configuration: AdUnitConfiguration.displayAdUnit(
109+
// TODO: Replace with your Ad Unit ID
110+
adSlot: '1234567890',
111+
isFullWidthResponsive: false,
112+
),
113+
),
105114
),
106115
],
107116
),

packages/google_adsense/lib/google_adsense.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'src/core/core.dart';
6-
export 'src/core/core.dart';
7-
8-
/// The singleton instance of the AdSense SDK.
9-
final AdSense adSense = AdSense();
5+
export 'src/core/google_adsense.dart';

packages/google_adsense/lib/src/adsense/ad_unit_configuration.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import 'package:flutter/foundation.dart';
66

77
import 'ad_unit_params.dart';
88

9-
/// Ad request configuration object.
10-
///
11-
/// Used to configure the ad widget request through the `adSense.adUnit` method
12-
/// (see: [AdUnitExtension]).
9+
/// Configuration to customize the [AdUnitWidget].
1310
///
1411
/// Contains named constructors for the following supported ad unit types:
1512
///

packages/google_adsense/lib/src/adsense/ad_unit_params.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class AdUnitParams {
4343
static const String AD_TEST = 'adtest';
4444
}
4545

46-
/// Possible values for [AdUnitParams.AD_FORMAT].
46+
/// Specifies the general shape that the ad unit should conform to.
4747
///
48-
/// See [docs](https://support.google.com/adsense/answer/9183460?hl=en&ref_topic=9183242&sjid=2004567335727763076-EU#:~:text=Specify%20a%20general%20shape%20(desktop%20only)) for details
48+
/// See [docs](https://support.google.com/adsense/answer/9183460?hl=en&ref_topic=9183242&sjid=2004567335727763076-EU#:~:text=Specify%20a%20general%20shape%20(desktop%20only)) for details.
4949
enum AdFormat {
5050
/// Default which enables the auto-sizing behavior for the responsive ad unit
5151
AUTO('auto'),
@@ -81,8 +81,7 @@ enum AdFormat {
8181
String toString() => _adFormat;
8282
}
8383

84-
/// Possible values for [AdUnitParams.AD_LAYOUT].
85-
///
84+
/// Controls the general layout of an in-feed/in-article ad unit.
8685
// TODO(sokoloff06): find docs link!
8786
enum AdLayout {
8887
///
@@ -107,9 +106,9 @@ enum AdLayout {
107106
String toString() => _adLayout;
108107
}
109108

110-
/// Possible values for [AdUnitParams.MATCHED_CONTENT_UI_TYPE].
109+
/// Controls the arrangement of the text and images in a Multiplex ad unit.
111110
///
112-
/// See [docs](https://support.google.com/adsense/answer/7533385?hl=en#:~:text=Change%20the%20layout%20of%20your%20Multiplex%20ad%20unit)
111+
/// See [docs](https://support.google.com/adsense/answer/7533385?hl=en#:~:text=Change%20the%20layout%20of%20your%20Multiplex%20ad%20unit).
113112
enum MatchedContentUiType {
114113
/// In this layout, the image and text appear alongside each other.
115114
IMAGE_CARD_SIDEBYSIDE('image_card_sidebyside'),

packages/google_adsense/lib/src/adsense/ad_unit_widget.dart

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,38 @@ import 'dart:js_interop_unsafe';
88
import 'package:flutter/widgets.dart';
99
import 'package:web/web.dart' as web;
1010

11+
import '../core/google_adsense.dart';
1112
import '../js_interop/adsbygoogle.dart';
12-
import '../logging.dart';
13+
import '../utils/logging.dart';
1314
import 'ad_unit_configuration.dart';
1415
import 'ad_unit_params.dart';
1516
import 'adsense_js_interop.dart';
1617

17-
/// Widget displaying an ad unit
18+
/// Widget displaying an ad unit.
19+
///
20+
/// See the [AdUnitConfiguration] object for a complete reference of the available
21+
/// parameters.
22+
///
23+
/// When specifying an [AdFormat], the widget will behave like a "responsive"
24+
/// ad unit. Responsive ad units might attempt to overflow the container in which
25+
/// they're rendered.
26+
///
27+
/// To create a fully constrained widget (specific width/height), do *not* pass
28+
/// an `AdFormat` value, and wrap the `AdUnitWidget` in a constrained box from
29+
/// Flutter, like a [Container] or [SizedBox].
1830
class AdUnitWidget extends StatefulWidget {
1931
/// Constructs [AdUnitWidget]
20-
const AdUnitWidget({
32+
AdUnitWidget({
2133
super.key,
22-
required String adClient,
2334
required AdUnitConfiguration configuration,
24-
}) : _adClient = adClient,
25-
_adUnitConfiguration = configuration;
35+
@visibleForTesting String? adClient,
36+
}) : _adClient = adClient ?? adSense.adClient,
37+
_adUnitConfiguration = configuration {
38+
assert(_adClient != null,
39+
'Attempted to render an AdUnitWidget before calling adSense.initialize');
40+
}
2641

27-
final String _adClient;
42+
final String? _adClient;
2843

2944
final AdUnitConfiguration _adUnitConfiguration;
3045

packages/google_adsense/lib/src/adsense/adsense.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
export 'ad_unit_configuration.dart';
66
export 'ad_unit_params.dart' hide AdStatus, AdUnitParams;
7-
export 'adsense_extension_stub.dart'
8-
if (dart.library.js_interop) 'adsense_extension.dart';
7+
export 'ad_unit_widget.dart';

0 commit comments

Comments
 (0)