forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AdSense Ad Placement API initial version
(This is a cherry-pick of PR flutter#7747)
- Loading branch information
Showing
16 changed files
with
558 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
# Below is a list of people and organizations that have contributed | ||
# to the Process project. Names should be added to the list like so: | ||
# | ||
# Name/Organization <email address> | ||
|
||
Google Inc. |
This file contains 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 @@ | ||
## 1.0.0 | ||
|
||
* Initial version |
This file contains 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 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,36 @@ | ||
# google_adsense_ad_placement_api_web | ||
|
||
Wrapper of the AdSense Ad Placement API to be used in Dart and Flutter apps. | ||
This Wrapper targets only the WEB platform. | ||
|
||
## Features | ||
|
||
To view the complete API, please check the official [AdSense website](https://developers.google.com/ad-placement/apis) | ||
|
||
## Getting started | ||
|
||
Add this package as a dependency on `pubspec.yaml` and import the library and the `ad_placement_api.dart` file. | ||
This package supports interacting with the Ad Placement API after it's already loaded. Please remember to include the appropriate adsense js files to the page before using this package. | ||
|
||
|
||
## Usage | ||
|
||
When you import the library inside a file, you'll have access to the singleton object `adPlacementApi`, which will have wrappers to both of the global functions that the Ad Placement API exposes: adBreak and adConfig. | ||
|
||
Example: | ||
<?code-excerpt "example/lib/main.dart (main-example)"?> | ||
```dart | ||
import 'package:google_adsense_ad_placement_api_web/google_adsense_ad_placement_api_web.dart'; | ||
void main() { | ||
adPlacementApi.adBreak( | ||
name: 'rewarded-example', | ||
type: BreakType.reward, | ||
); | ||
} | ||
``` | ||
|
||
## Testing | ||
|
||
Use `dart run script/tool/bin/flutter_plugin_tools.dart test --packages google_adsense_ad_placement_api_web --platform chrome` to test. | ||
You need to specify a web platform to be able to test the JS Interop library. |
14 changes: 14 additions & 0 deletions
14
packages/google_adsense_ad_placement_api_web/example/lib/main.dart
This file contains 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,14 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// #docregion main-example | ||
import 'package:google_adsense_ad_placement_api_web/google_adsense_ad_placement_api_web.dart'; | ||
|
||
void main() { | ||
adPlacementApi.adBreak( | ||
name: 'rewarded-example', | ||
type: BreakType.reward, | ||
); | ||
} | ||
// #enddocregion main-example |
21 changes: 21 additions & 0 deletions
21
packages/google_adsense_ad_placement_api_web/example/pubspec.yaml
This file contains 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 @@ | ||
name: google_adsense_ad_placement_api_web_example | ||
description: An example for the google_adsense_ad_placement_api_web_example package. | ||
publish_to: 'none' | ||
|
||
environment: | ||
sdk: ^3.4.0 | ||
flutter: ">=3.22.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
google_adsense_ad_placement_api_web: | ||
path: ../ | ||
web: ^1.0.0 | ||
|
||
dev_dependencies: | ||
build_runner: ^2.1.10 | ||
flutter_test: | ||
sdk: flutter | ||
integration_test: | ||
sdk: flutter |
12 changes: 12 additions & 0 deletions
12
packages/google_adsense_ad_placement_api_web/lib/google_adsense_ad_placement_api_web.dart
This file contains 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,12 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// AdSense Ad Placement API Library | ||
library; | ||
|
||
export 'src/ad_placement_api_js_interop.dart' | ||
show AdBreakDoneCallbackParamJSObject, AdBreakParamJSObject; | ||
export 'src/enums.dart' | ||
show BreakFormat, BreakStatus, BreakType, PreloadAdBreaks, SoundEnabled; | ||
export 'src/globals.dart' show adPlacementApi; |
107 changes: 107 additions & 0 deletions
107
packages/google_adsense_ad_placement_api_web/lib/src/ad_placement_api.dart
This file contains 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,107 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:js_interop'; | ||
|
||
import 'ad_placement_api_js_interop.dart'; | ||
import 'enums.dart'; | ||
|
||
/// Main class to interact with the Ad Placement API from Flutter Web/Dart code. | ||
class AdPlacementApi { | ||
/// Wraps JS Ad Placement API in a dart object. | ||
AdPlacementApi(this._adPlacementApiJsObject); | ||
|
||
final String _namePrefix = 'APFlutter-'; | ||
|
||
final AdPlacementApiJSObject? _adPlacementApiJsObject; | ||
|
||
/// Key function for placing ads within your app. It defines an ad placement and takes an object called a placement config that specifies everything required to show an ad. | ||
void adBreak({ | ||
/// The type of this placement | ||
required BreakType type, | ||
|
||
/// A descriptive name for this placement | ||
String? name, | ||
|
||
/// Prepare for the ad. Mute sounds and pause flow. | ||
void Function()? beforeAd, | ||
|
||
/// Resume the app flow and re-enable sound | ||
void Function()? afterAd, | ||
|
||
/// Show reward prompt (call showAdFn() if clicked) | ||
void Function(JSFunction showAdFn)? beforeReward, | ||
|
||
/// User dismissed the ad before completion | ||
void Function()? adDismissed, | ||
|
||
/// Ad was viewed and closed | ||
void Function()? adViewed, | ||
|
||
/// Always called (if provided) even if an ad didn't show | ||
void Function( | ||
BreakType? breakType, | ||
String? breakName, | ||
BreakFormat? breakFormat, | ||
BreakStatus? breakStatus, | ||
)? adBreakDone, | ||
}) { | ||
final AdBreakParamJSObject param = AdBreakParamJSObject(JSObject()); | ||
void empty() {} | ||
void showAdDefault(JSFunction? showAd) { | ||
if (showAd != null) { | ||
showAd.callAsFunction(); | ||
} | ||
} | ||
|
||
void wrappedAdBreakDoneCallback(AdBreakDoneCallbackParamJSObject param) { | ||
final BreakType type = BreakType.values.byName(param.breakType.toDart); | ||
final BreakFormat format = | ||
BreakFormat.values.byName(param.breakFormat.toDart); | ||
final BreakStatus status = | ||
BreakStatus.values.byName(param.breakStatus.toDart); | ||
final String name = param.breakName.toDart; | ||
if (adBreakDone != null) { | ||
adBreakDone(type, name, format, status); | ||
} | ||
} | ||
|
||
String breakName = _namePrefix; | ||
if (name != null) { | ||
breakName = breakName + name; | ||
} | ||
|
||
param.type = type.name.toJS; | ||
param.name = breakName.toJS; | ||
param.beforeAd = beforeAd != null ? beforeAd.toJS : empty.toJS; | ||
if (type != BreakType.preroll) { | ||
param.afterAd = afterAd != null ? afterAd.toJS : empty.toJS; | ||
} | ||
if (type == BreakType.reward) { | ||
param.beforeReward = | ||
beforeReward != null ? beforeReward.toJS : showAdDefault.toJS; | ||
param.adDismissed = adDismissed != null ? adDismissed.toJS : empty.toJS; | ||
param.adViewed = adViewed != null ? adViewed.toJS : empty.toJS; | ||
} | ||
param.adBreakDone = wrappedAdBreakDoneCallback.toJS; | ||
|
||
_adPlacementApiJsObject?.adBreak(param); | ||
} | ||
|
||
/// The adConfig() call communicates the app's current configuration to the Ad Placement API. | ||
/// The Ad Placement API can use this to tune the way it preloads ads and to filter the kinds of ads it requests so they're suitable (eg. video ads that require sound). | ||
void adConfig( | ||
PreloadAdBreaks preloadAdBreaks, | ||
SoundEnabled sound, | ||
void Function()? onReady, | ||
) { | ||
final AdConfigParamJSObject param = AdConfigParamJSObject(JSObject()); | ||
|
||
param.preloadAdBreaks = preloadAdBreaks.name.toJS; | ||
param.sound = sound.name.toJS; | ||
param.onReady = onReady?.toJS; | ||
|
||
_adPlacementApiJsObject?.adConfig(param); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/google_adsense_ad_placement_api_web/lib/src/ad_placement_api_js_interop.dart
This file contains 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,72 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:js_interop'; | ||
|
||
/// JSInterop object that wraps the Javascript object for the Ad Placement API. | ||
extension type AdPlacementApiJSObject(JSObject _) implements JSObject { | ||
/// Key function for placing ads within your app. It defines an ad placement and takes an object called a placement config that specifies everything required to show an ad. | ||
@JS() | ||
external void adBreak(AdBreakParamJSObject o); | ||
|
||
/// The adConfig() call communicates the app's current configuration to the Ad Placement API. | ||
/// The Ad Placement API can use this to tune the way it preloads ads and to filter the kinds of ads it requests so they're suitable (eg. video ads that require sound). | ||
@JS() | ||
external void adConfig(AdConfigParamJSObject o); | ||
} | ||
|
||
/// JSInterop object that wraps the parameters for the adBreak call in the Ad Placement API. | ||
extension type AdBreakParamJSObject(JSObject _) implements JSObject { | ||
/// The type of this placement | ||
external JSString type; | ||
|
||
/// A descriptive name for this placement | ||
external JSString? name; | ||
|
||
/// Prepare for the ad. Mute sounds and pause flow. | ||
external JSFunction? beforeAd; | ||
|
||
/// Resume the app flow and re-enable sound | ||
external JSFunction? afterAd; | ||
|
||
/// Show reward prompt (call showAdFn() if clicked) | ||
external JSFunction? beforeReward; | ||
|
||
/// User dismissed the ad before completion | ||
external JSFunction? adDismissed; | ||
|
||
/// Ad was viewed and closed | ||
external JSFunction? adViewed; | ||
|
||
/// Always called (if provided) even if an ad didn't show | ||
external JSFunction? adBreakDone; | ||
} | ||
|
||
/// JSInterop object representing the return object of the AdBreakDone callback | ||
extension type AdBreakDoneCallbackParamJSObject(JSObject _) | ||
implements JSObject { | ||
/// See [BreakType] enum | ||
external JSString breakType; | ||
|
||
/// Name of the ad break | ||
external JSString breakName; | ||
|
||
/// See [BreakFormat] enum | ||
external JSString breakFormat; | ||
|
||
/// See [BreakStatus] enum | ||
external JSString breakStatus; | ||
} | ||
|
||
/// JSInterop object that wraps the parameters for the adConfig call in the Ad Placement API. | ||
extension type AdConfigParamJSObject(JSObject _) implements JSObject { | ||
/// Ad preloading strategy | ||
external JSString preloadAdBreaks; | ||
|
||
/// This app has sound | ||
external JSString? sound; | ||
|
||
/// Called when API has initialised and adBreak() is ready | ||
external JSExportedDartFunction? onReady; | ||
} |
Oops, something went wrong.