Skip to content

Commit 2dc8b98

Browse files
authored
[google_adsense] Tighten exports and docs. (#8233)
* **Breaking changes**: Reshuffles API exports: * Removes the `adUnit` method, and instead exports the `AdUnitWidget` directly. * Renames `experimental/google_adsense` to `experimental/ad_unit_widget.dart`. * Removes the `AdStatus` and `AdUnitParams` exports. * Removes the "stub" files, so this package is now web-only and must be used through a conditional import. * Tweaks several documentation pages to remove references to internal APIs. * Splits tests to reflect the new code structure. ## Issue * Continuation of: #6871 * Part of: flutter/flutter#40376
1 parent 9f6d599 commit 2dc8b98

21 files changed

+489
-326
lines changed

packages/google_adsense/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## 0.0.2
2+
3+
* **Breaking changes**: Reshuffles API exports:
4+
* Makes `adSense.initialize` async.
5+
* Removes the `adUnit` method, and instead exports the `AdUnitWidget` directly.
6+
* Renames `experimental/google_adsense` to `experimental/ad_unit_widget.dart`.
7+
* Removes the `AdStatus` and `AdUnitParams` exports.
8+
* Removes the "stub" files, so this package is now web-only and must be used
9+
through a conditional import.
10+
* Tweaks several documentation pages to remove references to internal APIs.
11+
* Splits tests to reflect the new code structure.
12+
113
## 0.0.1
214

315
* Initial release.

packages/google_adsense/README.md

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,42 @@ Please express interest joining Early Access program using [this form](https://d
1010
## Usage
1111

1212
### Setup your AdSense account
13-
1. [Make sure your site's pages are ready for AdSense](https://support.google.com/adsense/answer/7299563?hl=en&sjid=5790642343077592212-EU&visit_id=638657100661171978-1373860041&ref_topic=1319756&rd=1)
14-
2. [Create your AdSense account](https://support.google.com/adsense/answer/10162?hl=en&sjid=5790642343077592212-EU&visit_id=638657100661171978-1373860041&ref_topic=1250103&rd=1)
13+
1. [Make sure your site's pages are ready for AdSense](https://support.google.com/adsense/answer/7299563)
14+
2. [Create your AdSense account](https://support.google.com/adsense/answer/10162)
1515

1616
### Initialize AdSense
17-
To start displaying ads, initialize the AdSense with your [client/publisher ID](https://support.google.com/adsense/answer/105516?hl=en&sjid=5790642343077592212-EU) (only use numbers).
17+
To start displaying ads, initialize AdSense with your [Publisher ID](https://support.google.com/adsense/answer/105516) (only use numbers).
18+
1819
<?code-excerpt "example/lib/main.dart (init)"?>
1920
```dart
20-
import 'package:google_adsense/experimental/google_adsense.dart';
21+
import 'package:google_adsense/experimental/ad_unit_widget.dart';
22+
import 'package:google_adsense/google_adsense.dart';
23+
24+
void main() async {
25+
// Call `initialize` with your Publisher ID (pub-0123456789012345)
26+
// (See: https://support.google.com/adsense/answer/105516)
27+
await adSense.initialize('0123456789012345');
2128
22-
void main() {
23-
adSense.initialize(
24-
'0123456789012345'); // TODO: Replace with your Publisher ID (pub-0123456789012345) - https://support.google.com/adsense/answer/105516?hl=en&sjid=5790642343077592212-EU
2529
runApp(const MyApp());
2630
}
27-
2831
```
2932

30-
### Enable Auto Ads
31-
In order to start displaying [Auto ads](https://support.google.com/adsense/answer/9261805?hl=en) 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):
35+
36+
1. Configure this feature in your AdSense Console.
3237

33-
### Display ad unit Widget
38+
Auto ads should start showing just with the call to `initialize`, when available.
3439

35-
1. Create [ad units](https://support.google.com/adsense/answer/9183549?hl=en&ref_topic=9183242&sjid=5790642343077592212-EU) in your AdSense account
36-
2. Use relevant `AdUnitConfiguration` constructor as per table below
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:
3749

3850
| Ad Unit Type | `AdUnitConfiguration` constructor method |
3951
|----------------|--------------------------------------------|
@@ -42,12 +54,17 @@ In order to start displaying [Auto ads](https://support.google.com/adsense/answe
4254
| In-article Ads | `AdUnitConfiguration.inArticleAdUnit(...)` |
4355
| Multiplex Ads | `AdUnitConfiguration.multiplexAdUnit(...)` |
4456

45-
3. Translate data-attributes from snippet generated in AdSense Console into constructor arguments as described below:
46-
- drop `data-` prefix
47-
- translate kebab-case to camelCase
48-
- no need to translate `data-ad-client` as it the value was already passed at initialization
57+
3. The data-attributes from the generated snippet are available through the `AdUnitConfiguration` object.
58+
Their Dart name is created as follows:
59+
60+
- The `data-` prefix is **removed**.
61+
- `kebab-case` becomes `camelCase`
62+
63+
The only exception to this is `data-ad-client`, that is passed to `adSense.initialize`,
64+
instead of through an `AdUnitConfiguration` object.
65+
66+
For example snippet below:
4967

50-
For example snippet below
5168
```html
5269
<ins class="adsbygoogle"
5370
style="display:block"
@@ -59,23 +76,32 @@ For example snippet below
5976
(adsbygoogle = window.adsbygoogle || []).push({});
6077
</script>
6178
```
62-
translates into
79+
translates into:
80+
6381
<?code-excerpt "example/lib/main.dart (init-min)"?>
6482
```dart
65-
adSense.initialize(
66-
'0123456789012345'); // TODO: Replace with your Publisher ID (pub-0123456789012345) - https://support.google.com/adsense/answer/105516?hl=en&sjid=5790642343077592212-EU
83+
// Call `initialize` with your Publisher ID (pub-0123456789012345)
84+
// (See: https://support.google.com/adsense/answer/105516)
85+
await adSense.initialize('0123456789012345');
86+
6787
```
68-
and
88+
89+
and:
90+
6991
<?code-excerpt "example/lib/main.dart (adUnit)"?>
7092
```dart
71-
adSense.adUnit(AdUnitConfiguration.displayAdUnit(
72-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
73-
adFormat: AdFormat
74-
.AUTO, // Remove AdFormat to make ads limited by height
75-
))
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+
),
76101
```
77102

78-
#### Customize ad unit Widget
103+
#### `AdUnitWidget` customizations
104+
79105
To [modify your responsive ad code](https://support.google.com/adsense/answer/9183363?hl=en&ref_topic=9183242&sjid=11551379421978541034-EU):
80106
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)
81107
2. Use Flutter instruments for [adaptive and responsive design](https://docs.flutter.dev/ui/adaptive-responsive)
@@ -89,11 +115,14 @@ Container(
89115
constraints:
90116
const BoxConstraints(maxHeight: 100, maxWidth: 1200),
91117
padding: const EdgeInsets.only(bottom: 10),
92-
child: adSense.adUnit(AdUnitConfiguration.displayAdUnit(
93-
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID
94-
adFormat: AdFormat
95-
.AUTO, // Not using AdFormat to make ad unit respect height constraint
96-
)),
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+
),
97126
),
98127
```
99128
## Testing and common errors
@@ -114,6 +143,7 @@ Make sure to set correct values to adSlot and adClient arguments
114143
### Ad unfilled
115144

116145
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
117147
- Try setting `adTest` parameter to `true`
118148
- Try setting AD_FORMAT to `auto` (default setting)
119149
- Try setting FULL_WIDTH_RESPONSIVE to `true` (default setting)

packages/google_adsense/example/integration_test/test_js_interop.dart renamed to packages/google_adsense/example/integration_test/adsense_test_js_interop.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:async';
99
import 'dart:js_interop';
1010
import 'dart:ui';
1111

12-
import 'package:google_adsense/experimental/google_adsense.dart';
12+
import 'package:google_adsense/src/adsense/ad_unit_params.dart';
1313
import 'package:web/web.dart' as web;
1414

1515
typedef VoidFn = void Function();
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// TO run the test:
6+
// 1. Run chrome driver with --port=4444
7+
// 2. Run the test from example folder with: flutter drive -d web-server --web-port 7357 --browser-name chrome --driver test_driver/integration_test.dart --target integration_test/ad_widget_test.dart
8+
9+
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:google_adsense/google_adsense.dart';
11+
import 'package:integration_test/integration_test.dart';
12+
import 'package:web/web.dart' as web;
13+
14+
import 'adsense_test_js_interop.dart';
15+
16+
const String testClient = 'test_client';
17+
const String testScriptUrl =
18+
'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-$testClient';
19+
20+
void main() async {
21+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
22+
23+
late AdSense adSense;
24+
25+
setUp(() async {
26+
adSense = AdSense();
27+
});
28+
29+
tearDown(() {
30+
clearAdsByGoogleMock();
31+
});
32+
33+
group('adSense.initialize', () {
34+
testWidgets('adds AdSense script tag.', (WidgetTester _) async {
35+
final web.HTMLElement target = web.HTMLDivElement();
36+
// Given
37+
38+
await adSense.initialize(testClient, jsLoaderTarget: target);
39+
40+
final web.HTMLScriptElement? injected =
41+
target.lastElementChild as web.HTMLScriptElement?;
42+
43+
expect(injected, isNotNull);
44+
expect(injected!.src, testScriptUrl);
45+
expect(injected.crossOrigin, 'anonymous');
46+
expect(injected.async, true);
47+
});
48+
49+
testWidgets('Skips initialization if script is already present.',
50+
(WidgetTester _) async {
51+
final web.HTMLScriptElement script = web.HTMLScriptElement()
52+
..id = 'previously-injected'
53+
..src = testScriptUrl;
54+
final web.HTMLElement target = web.HTMLDivElement()..appendChild(script);
55+
56+
await adSense.initialize(testClient, jsLoaderTarget: target);
57+
58+
expect(target.childElementCount, 1);
59+
expect(target.firstElementChild?.id, 'previously-injected');
60+
});
61+
62+
testWidgets('Skips initialization if adsense object is already present.',
63+
(WidgetTester _) async {
64+
final web.HTMLElement target = web.HTMLDivElement();
65+
66+
// Write an empty noop object
67+
mockAdsByGoogle(() {});
68+
69+
await adSense.initialize(testClient, jsLoaderTarget: target);
70+
71+
expect(target.firstElementChild, isNull);
72+
});
73+
});
74+
}

0 commit comments

Comments
 (0)