Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google_adsense] Adds H5 Games Ads support to package. #7747

Merged
merged 49 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2e049a9
AdSense Ad Placement API initial version
astivi Sep 30, 2024
ef746c7
Updates some comments, adds dart_test.yaml
astivi Oct 1, 2024
d8c70ee
Updates example
astivi Oct 1, 2024
1476e16
Adds AUTHORS, CHANGELOG and LICENSE files
astivi Oct 1, 2024
c64e711
Adds prefix to adBreak name
astivi Oct 1, 2024
c1da6cb
Merge branch 'main' into main
astivi Oct 1, 2024
b47969a
Renames to google_adsense_ad_placement_api_web
astivi Oct 1, 2024
5b98f15
Fixes some of the repo_checks
astivi Oct 1, 2024
c8edd29
Adds placeholder links for published package version
astivi Oct 1, 2024
85675ae
Change example to code-excerpt
astivi Oct 1, 2024
d3573ec
Merge branch 'main' into main
astivi Oct 1, 2024
b053a30
Adds issue tracker to pubspec.yaml
astivi Oct 1, 2024
31d4b6b
Fixes repository link
astivi Oct 1, 2024
9daf662
Exports main class to be able to be reused for different setups
astivi Oct 1, 2024
e942153
Fixes unused import issue
astivi Oct 1, 2024
30bfa73
Fixes preroll insertions
astivi Oct 2, 2024
f7e2d49
Merge branch 'main' into main
astivi Oct 2, 2024
208e3db
Merge branch 'main' into main
astivi Oct 2, 2024
334fd61
Merge branch 'main' into main
astivi Oct 7, 2024
a078a7c
Merge branch 'main' into main
astivi Oct 17, 2024
a54961a
Merge branch 'main' into main
astivi Oct 22, 2024
df8ed96
Merge branch 'main' into main
astivi Oct 24, 2024
62dffef
Merge branch 'main' into main
astivi Nov 18, 2024
1eed3de
Merge branch 'main' into HEAD
ditman Dec 4, 2024
8ab854a
Merge branch 'main' into main
ditman Dec 12, 2024
aa0adf6
Move h5 code from old package to google_adsense.
ditman Dec 6, 2024
8855333
Move h5 integration tests
ditman Dec 10, 2024
99fe604
Split example in init-adsense-h5
ditman Dec 10, 2024
66e7313
Only add non-null parameters to h5 request objects.
ditman Dec 10, 2024
1e490c5
Update README.md, needs H5 section.
ditman Dec 10, 2024
85e981a
Versioning
ditman Dec 10, 2024
ebfcf7d
Some tweaks to the example app.
ditman Dec 12, 2024
9cc071c
Do not wrap beforeReward unless it's passed in.
ditman Dec 12, 2024
1ec97d2
Prettify example
ditman Dec 12, 2024
ab8345f
dart format
ditman Dec 12, 2024
0511ab8
Update README and samples.
ditman Dec 12, 2024
659a16b
Remove google_adsense_ad_placement_api_web package.
ditman Dec 12, 2024
c79021c
Update CODEOWNERS
ditman Dec 12, 2024
3299c89
Remove old package from README
ditman Dec 12, 2024
441701c
Unbreak script injection tests.
ditman Dec 12, 2024
639193b
Add adBreakDone to interstitial example.
ditman Dec 12, 2024
4515aa1
Merge branch 'main' into main
ditman Dec 12, 2024
7a9c2ae
Split README into dartdoc TOPICS.
ditman Dec 12, 2024
660cf3c
Address PR comments.
ditman Dec 12, 2024
3bebda8
Final reviewed doc changes.
ditman Dec 13, 2024
08677d2
Correctly link code snippets after splitting the readme.
ditman Dec 13, 2024
7d44da6
Move js_interop to core/js_interop.
ditman Dec 13, 2024
bb49254
Renamed top level directory docs to doc, as instructed by the publish…
ditman Dec 13, 2024
b095321
Clarify h5 purpose.
ditman Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/google_adsense/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0

* Adds H5 Games Ads API as `h5` library.

## 0.0.2

* **Breaking changes**: Reshuffles API exports:
Expand Down
156 changes: 16 additions & 140 deletions packages/google_adsense/README.md
Original file line number Diff line number Diff line change
@@ -1,150 +1,26 @@
# google_adsense
[Google AdSense](https://adsense.google.com/intl/en_us/start/) plugin for Flutter Web

This package initializes AdSense on your website and provides an ad unit `Widget` that can be configured and placed in the desired location in your Flutter web app UI, without having to directly modify the HTML markup of the app directly.
[Google AdSense](https://adsense.google.com/start/) plugin for Flutter Web.

## Disclaimer: Early Access ⚠️
This package is currently in early access and is provided as-is. While it's open source and publicly available, it's likely that you'll need to make additional customizations and configurations to fully integrate it with your Flutter Web App.
Please express interest joining Early Access program using [this form](https://docs.google.com/forms/d/e/1FAIpQLSdN6aOwVkaxGdxbVQFVZ_N4_UCBkuWYa-cS4_rbU_f1jK10Tw/viewform)
This package provides a way to initialize and use AdSense on your Flutter Web app.
It includes libraries for the following products:

## Usage
* [H5 Games Ads](https://adsense.google.com/start/h5-games-ads/) (beta)
* (Experimental) [AdSense Ad Unit](https://support.google.com/adsense/answer/9183549) Widget

### Setup your AdSense account
1. [Make sure your site's pages are ready for AdSense](https://support.google.com/adsense/answer/7299563)
2. [Create your AdSense account](https://support.google.com/adsense/answer/10162)
## Documentation

### Initialize AdSense
To start displaying ads, initialize AdSense with your [Publisher ID](https://support.google.com/adsense/answer/105516) (only use numbers).
Check the [API docs](https://pub.dev/documentation/google_adsense/latest/)
to learn how to:

<?code-excerpt "example/lib/main.dart (init)"?>
```dart
import 'package:google_adsense/experimental/ad_unit_widget.dart';
import 'package:google_adsense/google_adsense.dart';
* [Initialize AdSense](https://pub.dev/documentation/google_adsense/latest/topics/Initialization-topic.html)
* [Use H5 Games Ads](https://pub.dev/documentation/google_adsense/latest/topics/H5%20Games%20Ads-topic.html)
* [Display Ad Units](https://pub.dev/documentation/google_adsense/latest/topics/Ad%20Units-topic.html)

void main() async {
// Call `initialize` with your Publisher ID (pub-0123456789012345)
// (See: https://support.google.com/adsense/answer/105516)
await adSense.initialize('0123456789012345');
## Support

runApp(const MyApp());
}
```
For any questions or support, please reach out to your Google representative or
leverage the [AdSense Help Center](https://support.google.com/adsense#topic=3373519).

### Displaying Auto Ads
In order to start displaying [Auto ads](https://support.google.com/adsense/answer/9261805):

1. Configure this feature in your AdSense Console.

Auto ads should start showing just with the call to `initialize`, when available.

If you want to display ad units within your app content, continue to the next step

### Display ad units (`AdUnitWidget`)

To display an Ad unit in your Flutter application:

1. Create [ad units](https://support.google.com/adsense/answer/9183549) in your AdSense account.
This will provide an HTML snippet, which you need to translate to Dart.
2. Pick the `AdUnitConfiguration` for your ad type:

| Ad Unit Type | `AdUnitConfiguration` constructor method |
|----------------|--------------------------------------------|
| Display Ads | `AdUnitConfiguration.displayAdUnit(...)` |
| In-feed Ads | `AdUnitConfiguration.inFeedAdUnit(...)` |
| In-article Ads | `AdUnitConfiguration.inArticleAdUnit(...)` |
| Multiplex Ads | `AdUnitConfiguration.multiplexAdUnit(...)` |

3. The data-attributes from the generated snippet are available through the `AdUnitConfiguration` object.
Their Dart name is created as follows:

- The `data-` prefix is **removed**.
- `kebab-case` becomes `camelCase`

The only exception to this is `data-ad-client`, that is passed to `adSense.initialize`,
instead of through an `AdUnitConfiguration` object.

For example snippet below:

```html
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-0123456789012345"
data-ad-slot="1234567890"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
```
translates into:

<?code-excerpt "example/lib/main.dart (init-min)"?>
```dart
// Call `initialize` with your Publisher ID (pub-0123456789012345)
// (See: https://support.google.com/adsense/answer/105516)
await adSense.initialize('0123456789012345');

```

and:

<?code-excerpt "example/lib/main.dart (adUnit)"?>
```dart
AdUnitWidget(
configuration: AdUnitConfiguration.displayAdUnit(
// TODO: Replace with your Ad Unit ID
adSlot: '1234567890',
// Remove AdFormat to make ads limited by height
adFormat: AdFormat.AUTO,
),
),
```

#### `AdUnitWidget` customizations

To [modify your responsive ad code](https://support.google.com/adsense/answer/9183363?hl=en&ref_topic=9183242&sjid=11551379421978541034-EU):
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)
2. Use Flutter instruments for [adaptive and responsive design](https://docs.flutter.dev/ui/adaptive-responsive)

For example, when not using responsive `AdFormat` it is recommended to wrap adUnit widget in the `Container` with width and/or height constraints.
Note some [policies and restrictions](https://support.google.com/adsense/answer/9185043?hl=en#:~:text=Policies%20and%20restrictions) related to ad unit sizing:

<?code-excerpt "example/lib/main.dart (constraints)"?>
```dart
Container(
constraints:
const BoxConstraints(maxHeight: 100, maxWidth: 1200),
padding: const EdgeInsets.only(bottom: 10),
child: AdUnitWidget(
configuration: AdUnitConfiguration.displayAdUnit(
// TODO: Replace with your Ad Unit ID
adSlot: '1234567890',
// Do not use adFormat to make ad unit respect height constraint
// adFormat: AdFormat.AUTO,
),
),
),
```
## Testing and common errors

### Failed to load resource: the server responded with a status of 400
Make sure to set correct values to adSlot and adClient arguments

### Failed to load resource: the server responded with a status of 403
1. When happening in **testing/staging** environment it is likely related to the fact that ads are only filled when requested from an authorized domain. If you are testing locally and running your web app on `localhost`, you need to:
1. Set custom domain name on localhost by creating a local DNS record that would point `127.0.0.1` and/or `localhost` to `your-domain.com`. On mac/linux machines this can be achieved by adding the following records to you /etc/hosts file:
`127.0.0.1 your-domain.com`
`localhost your-domain.com`
2. Specify additional run arguments in IDE by editing `Run/Debug Configuration` or by passing them directly to `flutter run` command:
`--web-port=8080`
`--web-hostname=your-domain.com`
2. When happening in **production** it might be that your domain was not yet approved or was disapproved. Login to your AdSense account to check your domain approval status

### Ad unfilled

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:
- Ensure your ad units are correctly configured in AdSense
- Try setting `adTest` parameter to `true`
- Try setting AD_FORMAT to `auto` (default setting)
- Try setting FULL_WIDTH_RESPONSIVE to `true` (default setting)
- Try resizing the window or making sure that ad unit Widget width is less than ~1200px
For problem with this package, please
[create a Github issue](https://github.com/flutter/flutter/issues/new?assignees=&labels=&projects=&template=9_first_party_packages.yml).
13 changes: 13 additions & 0 deletions packages/google_adsense/dartdoc_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dartdoc:
categories:
"Initialization":
markdown: docs/initialization.md
"H5 Games Ads":
markdown: docs/h5.md
"Ad Units":
markdown: docs/ad_unit_widget.md
categoryOrder:
- "Initialization"
- "H5 Games Ads"
- "Ad Units"
showUndocumentedCategories: true
143 changes: 143 additions & 0 deletions packages/google_adsense/docs/ad_unit_widget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# `AdUnitWidget`

The `experimental/ad_unit_widget.dart` library provides an ad unit `Widget` that
can be configured and placed in the widget tree of your Flutter web app.

## Disclaimer: Early Access ⚠️
This package is currently in early access and is provided as-is. While it's open
source and publicly available, it's likely that you'll need to make additional
customizations and configurations to fully integrate it with your Flutter Web App.

The early access list is a closed one for now, stay tuned for expanded availability
of the Ad Unit Widget for Flutter web.

## Usage

First, initialize AdSense (see the
[Initialization](https://pub.dev/documentation/google_adsense/latest/topics/Initialization-topic.html)
topic).

### Displaying Auto Ads

In order to start displaying [Auto ads](https://support.google.com/adsense/answer/9261805):

1. Configure this feature in your AdSense Console.

Auto ads should start showing just with the call to `initialize`, when available.

If you want to display ad units within your app content, continue to the next steps:

### Import the widget

Import the **experimental** `AdUnitWidget` from the package:

<?code-excerpt "example/lib/ad_unit_widget.dart (import-widget)"?>
```dart
import 'package:google_adsense/experimental/ad_unit_widget.dart';
```

### Display ad units (`AdUnitWidget`)

To display an Ad unit in your Flutter application:

1. Create [ad units](https://support.google.com/adsense/answer/9183549)
in your AdSense account. This will provide an HTML snippet, which you need to
_translate_ to Dart.
2. The data-attributes from the generated snippet can be translated to Dart with the `AdUnitConfiguration` object.
Their Dart name is created as follows:

- The `data-` prefix is removed.
- `kebab-case` becomes `camelCase`

The only exception to this is `data-ad-client`, that is passed to `adSense.initialize`,
instead of through an `AdUnitConfiguration` object.

For example, the snippet below:

```html
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-0123456789012345"
data-ad-slot="1234567890"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
```

translates into:

<?code-excerpt "example/lib/ad_unit_widget.dart (adUnit)"?>
```dart
AdUnitWidget(
configuration: AdUnitConfiguration.displayAdUnit(
// TODO: Replace with your Ad Unit ID
adSlot: '1234567890',
// Remove AdFormat to make ads limited by height
adFormat: AdFormat.AUTO,
),
),
```

#### `AdUnitConfiguration` constructors

In addition to `displayAdUnit`, there's specific constructors for each supported
Ad Unit type. See the table below:

| Ad Unit Type | `AdUnitConfiguration` constructor method |
|----------------|--------------------------------------------|
| Display Ads | `AdUnitConfiguration.displayAdUnit(...)` |
| In-feed Ads | `AdUnitConfiguration.inFeedAdUnit(...)` |
| In-article Ads | `AdUnitConfiguration.inArticleAdUnit(...)` |
| Multiplex Ads | `AdUnitConfiguration.multiplexAdUnit(...)` |


#### `AdUnitWidget` customizations

To [modify your responsive ad code](https://support.google.com/adsense/answer/9183363?hl=en&ref_topic=9183242&sjid=11551379421978541034-EU):
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)
2. Use Flutter instruments for [adaptive and responsive design](https://docs.flutter.dev/ui/adaptive-responsive)

For example, when not using responsive `AdFormat` it is recommended to wrap adUnit widget in the `Container` with width and/or height constraints.
Note some [policies and restrictions](https://support.google.com/adsense/answer/9185043?hl=en#:~:text=Policies%20and%20restrictions) related to ad unit sizing:

<?code-excerpt "example/lib/ad_unit_widget.dart (constraints)"?>
```dart
Container(
constraints:
const BoxConstraints(maxHeight: 100, maxWidth: 1200),
padding: const EdgeInsets.only(bottom: 10),
child: AdUnitWidget(
configuration: AdUnitConfiguration.displayAdUnit(
// TODO: Replace with your Ad Unit ID
adSlot: '1234567890',
// Do not use adFormat to make ad unit respect height constraint
// adFormat: AdFormat.AUTO,
),
),
),
```
## Testing and common errors

### Failed to load resource: the server responded with a status of 400
Make sure to set correct values to adSlot and adClient arguments

### Failed to load resource: the server responded with a status of 403
1. When happening in **testing/staging** environment it is likely related to the fact that ads are only filled when requested from an authorized domain. If you are testing locally and running your web app on `localhost`, you need to:
1. Set custom domain name on localhost by creating a local DNS record that would point `127.0.0.1` and/or `localhost` to `your-domain.com`. On mac/linux machines this can be achieved by adding the following records to you /etc/hosts file:
`127.0.0.1 your-domain.com`
`localhost your-domain.com`
2. Specify additional run arguments in IDE by editing `Run/Debug Configuration` or by passing them directly to `flutter run` command:
`--web-port=8080`
`--web-hostname=your-domain.com`
2. When happening in **production** it might be that your domain was not yet approved or was disapproved. Login to your AdSense account to check your domain approval status

### Ad unfilled

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:
- Ensure your ad units are correctly configured in AdSense
- Try setting `adTest` parameter to `true`
- Try setting AD_FORMAT to `auto` (default setting)
- Try setting FULL_WIDTH_RESPONSIVE to `true` (default setting)
- Try resizing the window or making sure that ad unit Widget width is less than ~1200px
Loading
Loading