-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[google_adsense] (Experimental) AdSense plugin for Flutter Web #6871
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
Merged
Merged
Changes from all commits
Commits
Show all changes
113 commits
Select commit
Hold shift + click to select a range
ddb2de6
Initial version of AdSense plugin for Flutter Web
sokoloff06 968dcdb
Merge branch 'flutter:main' into main
sokoloff06 3c99bae
Merge branch 'flutter:main' into main
sokoloff06 c37d644
WIP: apply flutter_packages analysis_options.yaml and fixing warnings
sokoloff06 dfae8ee
Merge remote-tracking branch 'origin/main'
sokoloff06 15826c0
remove local analysis_options.yaml
sokoloff06 6050087
Update README.md, pubspec.yaml, work on example and linter warnings
sokoloff06 25158d3
Update public API, add example, update README.md, add more docs and s…
sokoloff06 91ae43f
Update public API, add example, update README.md, add more docs and s…
sokoloff06 4308c93
Merge remote-tracking branch 'origin/main'
sokoloff06 52fb128
Merge remote-tracking branch 'origin/main'
sokoloff06 a562233
Merge remote-tracking branch 'origin/main'
sokoloff06 500483d
Fix analyzer warnings
sokoloff06 65c61ab
Updated README.md
sokoloff06 065e6fe
Add documentation, copyright, initial tests, update README.md
sokoloff06 b99f4f7
Fix linter error
sokoloff06 2c7db93
Make plugin closer to publishable state: update copyright, commit mis…
sokoloff06 1697032
Rename adView to adUnit, fix deps and update README.md
sokoloff06 875f621
Merge branch 'main' into main
sokoloff06 5773e5d
Refactoring + unit tests
sokoloff06 537d611
Merge remote-tracking branch 'origin/main'
sokoloff06 f105166
Refactoring + unit tests
sokoloff06 7d3b9c2
Upgrade dart SDK
sokoloff06 de1fe7c
Upgrade package:web and dart
sokoloff06 458cb4e
Add integration test
sokoloff06 ffab481
Clarify 403 error in README.md
sokoloff06 257007f
Rename ad_unit_widget.dart and solve dart analysis errors
sokoloff06 7701a7c
Merge branch 'main' into main
sokoloff06 172dd00
Add styling property for adUnit CSS customization
sokoloff06 c427a8a
Merge remote-tracking branch 'origin/main'
sokoloff06 689b1a6
Merge branch 'main' into main
sokoloff06 215da90
Code format
sokoloff06 3232f0e
Fix repo rules
sokoloff06 d59f7b7
Fix repo rules
sokoloff06 98b0845
Update min Dart version and readme links
sokoloff06 0232565
Update min SDK versions for dependencies compatibility
sokoloff06 f2b9c06
Fix typo
sokoloff06 db13a98
Revert README.md
sokoloff06 51e2042
Update README.md
sokoloff06 c57e03f
Merge branch 'main' into main
sokoloff06 e6842f2
Update README.md, rename class Adsense into AdSense
sokoloff06 913f347
Update packages/google_adsense/example/pubspec.yaml
sokoloff06 9ba0ba5
Use instance property to access AdSense singleton instance
sokoloff06 eb20328
Merge remote-tracking branch 'sokoloff06/main' into fork/sokoloff06/main
sokoloff06 2a8a998
Add explanation about local DNS records for testing environment
sokoloff06 1c29983
Use strong String type for additionalParams argument values
sokoloff06 d07ea36
Remove as JS* casts, change isAdTest default value, improve some docs…
sokoloff06 d48aa16
Remove as JS* casts, change isAdTest default value, improve some docs…
sokoloff06 094460b
Add screenshots to pubspec.yaml
sokoloff06 3f58513
Simplify ResizeObserver, define adsbygoogle JS variable
sokoloff06 988d3f7
Code cleanup
sokoloff06 6c244fd
Merge tests
sokoloff06 410a7eb
Tests cleanup
sokoloff06 dc78f2e
Provide adSense global variable for convenience
sokoloff06 12e657d
Remove adClient override logic,
sokoloff06 3a820bc
Update adUnit API with AdUnitConfiguration and named constructors for…
sokoloff06 1d288db
Remove test_ad.html, format code
sokoloff06 d214fc0
Merge branch 'main' into main
sokoloff06 3fda3da
Ignore avoid_print in test, remove redundant arguments
sokoloff06 0258b86
use testWidgets in integration tests
sokoloff06 b9f61b8
Fix adUnit returns Widget type test
sokoloff06 c509169
Update README.md and add LICENSE
sokoloff06 4096469
Tweak JS-interop.
ditman fbde1c2
Tweak ad unit param enums.
ditman bbaa9db
Use new js-interop init. Remove array fallback from addMasterScript.
ditman 9742dcf
Use new types in ad_unit_configuration
ditman 3acd43c
Use Size for ad unit widget. Some tweaks.
ditman bc140f4
dart format .
ditman bd7f839
Fix HTMLDivElement cast
ditman 604b87b
Use offsetWidth/height
ditman 4175da6
Fix tests
ditman 36d2550
Merge pull request #1 from ditman/adsense-js-interop
sokoloff06 875f72c
Remove width and height css rules, fix test app
sokoloff06 41edb56
TO_REVIEW: Optimize? - Reuse single static ResizeObserver as per [thi…
sokoloff06 39dfc97
Hide logs from release, add AdStatus enum,
sokoloff06 1bbaeee
Fix AdStatus
sokoloff06 7a31b8e
Fix unit tests CI
sokoloff06 aeab558
Add TrustedTypes support
sokoloff06 3235d27
Remove stub tests
sokoloff06 5a7abad
Fix missing type annotation
sokoloff06 9dca5d1
Update README.md excerpts
sokoloff06 7d828cf
Update example to more complex layout
sokoloff06 d9dbc79
Merge branch 'main' into main
sokoloff06 308a325
Simplify API: remove cssText and add docs to use Flutter instruments,…
sokoloff06 baac6ba
Tweak initialization. Support prior initialization in HTML.
ditman 2d9e5bd
Add integration tests mocking window.adsbygoogle. Tweaks.
ditman 456da08
Ensure package compiles in Wasm.
ditman 5f2b324
Tweak the API of AdSense. Rename internal adClient to adClientId. Mak…
ditman ffd8c4a
Remove adClient from AdUnitParams object, because it's always passed …
ditman 7e4ddc9
Pass clientId when creating ad units from AdSense instance.
ditman d2b42f1
Simplify some more code.
ditman e5da3bf
Make isAdTest a normal member of the config. Remove unused getters.
ditman 9ce3347
Remove AdUnitWidget._internal constructor.
ditman 8bda3fd
Make as much API private as possible.
ditman 500337e
Use new constructor instead of the removed factory.
ditman 06886dd
Allow limiting ads by height, update example
sokoloff06 a36f9f8
Allow limiting ads by height, update example
sokoloff06 7f55807
Merge pull request #2 from ditman/add-mock-adsense-tests
sokoloff06 ba38273
Fix CI analyzer fails
sokoloff06 1914f6b
Fix CI analyzer fails
sokoloff06 c5e46a6
Fix CI analyzer fails
sokoloff06 3bb56b9
Make sure <ins> initial size is equal to wrapping div
sokoloff06 d7abe89
dart format
sokoloff06 a92864f
Add docs on using BoxConstraints
sokoloff06 4e52e92
Update tests to match AdSense behavior, and our code.
ditman 44fc9e6
Update README.md and comments
sokoloff06 49a7a75
Merge pull request #3 from ditman/fix-ins-in-integration-test
sokoloff06 683fe65
Update README.md
sokoloff06 06803b8
Update README.md
sokoloff06 c0d9ec3
Merge remote-tracking branch 'sokoloff06/main' into fork/sokoloff06/main
sokoloff06 6dbecd7
Merge branch 'main' into main
sokoloff06 53f47e4
Move exports to experimental folder, update README.md
sokoloff06 9735ba0
Fix CI
sokoloff06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Below is a list of people and organizations that have contributed | ||
# to the Flutter project. Names should be added to the list like so: | ||
# | ||
# Name/Organization <email address> | ||
|
||
Google Inc. | ||
The Chromium Authors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.0.1 | ||
|
||
* Initial release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright 2013 The Flutter Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# 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. | ||
|
||
## 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) | ||
|
||
## Usage | ||
|
||
### Setup your AdSense account | ||
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) | ||
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) | ||
|
||
### Initialize AdSense | ||
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). | ||
<?code-excerpt "example/lib/main.dart (init)"?> | ||
sokoloff06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```dart | ||
import 'package:google_adsense/experimental/google_adsense.dart'; | ||
|
||
void main() { | ||
adSense.initialize( | ||
'0123456789012345'); // TODO: Replace with your Publisher ID (pub-0123456789012345) - https://support.google.com/adsense/answer/105516?hl=en&sjid=5790642343077592212-EU | ||
runApp(const MyApp()); | ||
} | ||
|
||
``` | ||
|
||
### Enable Auto Ads | ||
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 | ||
|
||
### Display ad unit Widget | ||
|
||
1. Create [ad units](https://support.google.com/adsense/answer/9183549?hl=en&ref_topic=9183242&sjid=5790642343077592212-EU) in your AdSense account | ||
2. Use relevant `AdUnitConfiguration` constructor as per 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(...)` | | ||
|
||
3. Translate data-attributes from snippet generated in AdSense Console into constructor arguments as described below: | ||
- drop `data-` prefix | ||
- translate kebab-case to camelCase | ||
- no need to translate `data-ad-client` as it the value was already passed at initialization | ||
|
||
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 | ||
adSense.initialize( | ||
'0123456789012345'); // TODO: Replace with your Publisher ID (pub-0123456789012345) - https://support.google.com/adsense/answer/105516?hl=en&sjid=5790642343077592212-EU | ||
``` | ||
and | ||
<?code-excerpt "example/lib/main.dart (adUnit)"?> | ||
```dart | ||
adSense.adUnit(AdUnitConfiguration.displayAdUnit( | ||
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID | ||
adFormat: AdFormat | ||
.AUTO, // Remove AdFormat to make ads limited by height | ||
)) | ||
``` | ||
|
||
#### Customize ad unit Widget | ||
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: adSense.adUnit(AdUnitConfiguration.displayAdUnit( | ||
adSlot: '1234567890', // TODO: Replace with your Ad Unit ID | ||
adFormat: AdFormat | ||
.AUTO, // Not using AdFormat to make ad unit respect height constraint | ||
)), | ||
), | ||
``` | ||
## 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: | ||
- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## See https://github.com/dart-lang/test/blob/master/pkgs/test/doc/configuration.md#arguments | ||
#override_platforms: | ||
# chrome: | ||
# settings: | ||
# executable: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome | ||
# arguments: --no-sandbox |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# google_adsense_example | ||
|
||
An example demonstrating google_adsense Flutter plugin usage | ||
|
||
## Screenshots | ||
|
||
 | ||
sokoloff06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
 | ||
|
||
|
||
|
||
## Testing | ||
|
||
This package uses `package:integration_test` to run its tests in a web browser. | ||
|
||
See [Plugin Tests > Web Tests](https://github.com/flutter/flutter/blob/master/docs/ecosystem/testing/Plugin-Tests.md#web-tests) | ||
in the Flutter documentation for instructions to set up and run the tests in this package. | ||
|
||
Check [flutter.dev > Integration testing](https://docs.flutter.dev/testing/integration-tests) | ||
for more info. | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.