forked from firebase/flutterfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[firebase_analytics_web] Add initial implementation for firebase_anal…
…ytics_web (firebase#2573)
- Loading branch information
Showing
8 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/firebase_analytics/firebase_analytics_web/.gitignore
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,75 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
**/doc/api/ | ||
.dart_tool/ | ||
.flutter-plugins | ||
.flutter-plugins-dependencies | ||
.packages | ||
.pub-cache/ | ||
.pub/ | ||
build/ | ||
|
||
# Android related | ||
**/android/**/gradle-wrapper.jar | ||
**/android/.gradle | ||
**/android/captures/ | ||
**/android/gradlew | ||
**/android/gradlew.bat | ||
**/android/local.properties | ||
**/android/**/GeneratedPluginRegistrant.java | ||
|
||
# iOS/XCode related | ||
**/ios/**/*.mode1v3 | ||
**/ios/**/*.mode2v3 | ||
**/ios/**/*.moved-aside | ||
**/ios/**/*.pbxuser | ||
**/ios/**/*.perspectivev3 | ||
**/ios/**/*sync/ | ||
**/ios/**/.sconsign.dblite | ||
**/ios/**/.tags* | ||
**/ios/**/.vagrant/ | ||
**/ios/**/DerivedData/ | ||
**/ios/**/Icon? | ||
**/ios/**/Pods/ | ||
**/ios/**/.symlinks/ | ||
**/ios/**/profile | ||
**/ios/**/xcuserdata | ||
**/ios/.generated/ | ||
**/ios/Flutter/App.framework | ||
**/ios/Flutter/Flutter.framework | ||
**/ios/Flutter/Flutter.podspec | ||
**/ios/Flutter/Generated.xcconfig | ||
**/ios/Flutter/app.flx | ||
**/ios/Flutter/app.zip | ||
**/ios/Flutter/flutter_assets/ | ||
**/ios/Flutter/flutter_export_environment.sh | ||
**/ios/ServiceDefinitions.json | ||
**/ios/Runner/GeneratedPluginRegistrant.* | ||
|
||
# Exceptions to above rules. | ||
!**/ios/**/default.mode1v3 | ||
!**/ios/**/default.mode2v3 | ||
!**/ios/**/default.pbxuser | ||
!**/ios/**/default.perspectivev3 | ||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages |
10 changes: 10 additions & 0 deletions
10
packages/firebase_analytics/firebase_analytics_web/.metadata
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,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 0da1ab09224f6c6d69fcff1195a3662fe7ad7534 | ||
channel: beta | ||
|
||
project_type: package |
3 changes: 3 additions & 0 deletions
3
packages/firebase_analytics/firebase_analytics_web/CHANGELOG.md
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 @@ | ||
## 0.1.0 | ||
|
||
- Initial implementation for firebase_analytics_web. |
27 changes: 27 additions & 0 deletions
27
packages/firebase_analytics/firebase_analytics_web/LICENSE
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,27 @@ | ||
// Copyright 2017 The Chromium 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. |
83 changes: 83 additions & 0 deletions
83
packages/firebase_analytics/firebase_analytics_web/README.md
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,83 @@ | ||
# firebase_analytics_web | ||
|
||
The web implementation of [firebase_analytics][1] | ||
|
||
## Usage | ||
|
||
### Import the package | ||
|
||
This package is the endorsed implementation of `firebase_analytics` for the web platform since version `5.0.16`, so it gets automatically added to your application by depending on `firebase_analytics: ^5.0.16`. | ||
|
||
No further modifications to your `pubspec.yaml` should be required in a recent enough version of Flutter (`>=1.12.13+hotfix.4`): | ||
|
||
```yaml | ||
... | ||
dependencies: | ||
... | ||
firebase_analytics: ^5.0.16 | ||
... | ||
``` | ||
|
||
### Update `index.html` | ||
|
||
Due to [this bug in dartdevc][2], you will need to manually add the Firebase | ||
JavaScript files to your `index.html` file. | ||
|
||
In your app directory, edit `web/index.html` to add the following: | ||
|
||
```html | ||
<html> | ||
... | ||
<body> | ||
<script src="https://www.gstatic.com/firebasejs/7.14.3/firebase-app.js"></script> | ||
<script src="https://www.gstatic.com/firebasejs/7.14.3/firebase-analytics.js"></script> | ||
<!-- Other firebase SDKs/config here --> | ||
<script src="main.dart.js"></script> | ||
</body> | ||
</html> | ||
``` | ||
|
||
### Initialize Firebase | ||
|
||
If your app is using the "default" Firebase app _(this means that you're not doing any `package:firebase_core` initialization yourself)_, | ||
you'll need to initialize it now, following the steps in the [Firebase Web Setup][3] docs. | ||
|
||
Specifically, you'll want to add the following lines to your `web/index.html` file: | ||
|
||
```html | ||
<body> | ||
<!-- Previously loaded Firebase SDKs --> | ||
|
||
<!-- ADD THIS BEFORE YOUR main.dart.js SCRIPT --> | ||
<script> | ||
// TODO: Replace the following with your app's Firebase project configuration. | ||
// See: https://support.google.com/firebase/answer/7015592 | ||
var firebaseConfig = { | ||
apiKey: "...", | ||
authDomain: "[YOUR_PROJECT].firebaseapp.com", | ||
databaseURL: "https://[YOUR_PROJECT].firebaseio.com", | ||
projectId: "[YOUR_PROJECT]", | ||
storageBucket: "[YOUR_PROJECT].appspot.com", | ||
messagingSenderId: "...", | ||
appId: "1:...:web:...", | ||
measurementId: "G-..." | ||
}; | ||
// Initialize Firebase | ||
firebase.initializeApp(firebaseConfig); | ||
// Initialize Analytics | ||
firebase.analytics(); | ||
</script> | ||
<!-- END OF FIREBASE INIT CODE --> | ||
|
||
<script src="main.dart.js"></script> | ||
</body> | ||
``` | ||
|
||
### Use the plugin | ||
|
||
Once you have modified your `web/index.html` file, you should be able to use `package:firebase_analytics` as normal. Refer to the [`firebase_analytics` documentation][4] for more details. | ||
|
||
[1]: https://pub.dev/packages/firebase_analytics | ||
[2]: https://github.com/dart-lang/sdk/issues/33979 | ||
[3]: https://firebase.google.com/docs/web/setup#add-sdks-initialize | ||
[4]: ../firebase_analytics |
53 changes: 53 additions & 0 deletions
53
packages/firebase_analytics/firebase_analytics_web/lib/firebase_analytics_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,53 @@ | ||
import 'package:firebase/firebase.dart' as firebase; | ||
import 'package:firebase_analytics_platform_interface/firebase_analytics_platform_interface.dart'; | ||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Web implementation for [FirebaseAnalyticsPlatform] | ||
class FirebaseAnalyticsWeb extends FirebaseAnalyticsPlatform { | ||
/// Instance of Analytics from the web plugin. | ||
final firebase.Analytics _analytics; | ||
|
||
/// A constructor that allows tests to override the firebase.Analytics object. | ||
FirebaseAnalyticsWeb({@visibleForTesting firebase.Analytics analytics}) | ||
: _analytics = analytics ?? firebase.analytics(); | ||
|
||
/// Called by PluginRegistry to register this plugin for Flutter Web | ||
static void registerWith(Registrar registrar) { | ||
FirebaseAnalyticsPlatform.instance = FirebaseAnalyticsWeb(); | ||
} | ||
|
||
@override | ||
Future<void> logEvent({ | ||
String name, | ||
Map<String, dynamic> parameters, | ||
}) async { | ||
_analytics.logEvent(name, parameters); | ||
} | ||
|
||
@override | ||
Future<void> setAnalyticsCollectionEnabled(bool enabled) async { | ||
_analytics.setAnalyticsCollectionEnabled(enabled); | ||
} | ||
|
||
@override | ||
Future<void> setUserId(String id) async { | ||
_analytics.setUserId(id); | ||
} | ||
|
||
@override | ||
Future<void> setCurrentScreen({ | ||
String screenName, | ||
String screenClassOverride, | ||
}) async { | ||
_analytics.setCurrentScreen(screenName); | ||
} | ||
|
||
@override | ||
Future<void> setUserProperty({ | ||
String name, | ||
String value, | ||
}) async { | ||
_analytics.setUserProperties({name: value}); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/firebase_analytics/firebase_analytics_web/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,29 @@ | ||
name: firebase_analytics_web | ||
description: The web implementation of firebase_analytics | ||
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_analytics/firebase_analytics_web | ||
version: 0.1.0 | ||
|
||
flutter: | ||
plugin: | ||
platforms: | ||
web: | ||
pluginClass: FirebaseAnalyticsWeb | ||
fileName: firebase_analytics_web.dart | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
flutter_web_plugins: | ||
sdk: flutter | ||
firebase: ^7.3.0 | ||
firebase_analytics_platform_interface: ^1.0.1 | ||
meta: ^1.1.8 | ||
|
||
dev_dependencies: | ||
flutter_test: | ||
sdk: flutter | ||
mockito: ^4.1.1 | ||
|
||
environment: | ||
sdk: ">=2.7.0 <3.0.0" | ||
flutter: ">=1.12.13+hotfix.4 <2.0.0" |
57 changes: 57 additions & 0 deletions
57
packages/firebase_analytics/firebase_analytics_web/test/firebase_analytics_web_test.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,57 @@ | ||
@TestOn('chrome') // Uses web-only Flutter SDK | ||
|
||
import 'package:firebase/firebase.dart'; | ||
import 'package:firebase_analytics_web/firebase_analytics_web.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
class MockAnalytics extends Mock implements Analytics {} | ||
|
||
void main() { | ||
group('FirebaseAnalyticsWeb', () { | ||
FirebaseAnalyticsWeb firebaseAnalytics; | ||
MockAnalytics analytics; | ||
|
||
setUp(() { | ||
analytics = MockAnalytics(); | ||
firebaseAnalytics = FirebaseAnalyticsWeb(analytics: analytics); | ||
}); | ||
|
||
test('logEvent', () async { | ||
final name = 'random'; | ||
final parameters = {'a': 'b'}; | ||
await firebaseAnalytics.logEvent(name: name, parameters: parameters); | ||
verify(analytics.logEvent(name, parameters)); | ||
}); | ||
|
||
test('setAnalyticsCollectionEnabled', () async { | ||
final enabled = true; | ||
await firebaseAnalytics.setAnalyticsCollectionEnabled(enabled); | ||
verify(analytics.setAnalyticsCollectionEnabled(enabled)); | ||
}); | ||
|
||
test('setUserId', () async { | ||
final userId = 'userId'; | ||
await firebaseAnalytics.setUserId(userId); | ||
verify(analytics.setUserId(userId)); | ||
}); | ||
|
||
test('setCurrentScreen', () async { | ||
final screenName = 'screenName'; | ||
// screenClassOverride is discarded in web. | ||
final screenClassOverride = 'screenClassOverride'; | ||
await firebaseAnalytics.setCurrentScreen( | ||
screenName: screenName, | ||
screenClassOverride: screenClassOverride, | ||
); | ||
verify(analytics.setCurrentScreen(screenName)); | ||
}); | ||
|
||
test('setUserProperty', () async { | ||
final name = 'name'; | ||
final value = 'value'; | ||
await firebaseAnalytics.setUserProperty(name: name, value: value); | ||
verify(analytics.setUserProperties({name: value})); | ||
}); | ||
}); | ||
} |