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

Migrate from dart:html to package:web #79

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 16 additions & 16 deletions packages/core/lib/analytics_web.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:async';
import 'dart:html' as html show window;

import 'package:segment_analytics/native_context.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'package:segment_analytics/analytics_platform_interface.dart';
import 'package:segment_analytics/native_context.dart';
import 'package:web/web.dart' as web;

export 'package:segment_analytics/client.dart';

Expand All @@ -15,19 +14,20 @@ class AnalyticsPlatformImpl extends AnalyticsPlatform {

/// Returns a [String] containing the version of the platform.
@override
Future<NativeContext> getContext({bool collectDeviceId = false}) =>
Future.value(NativeContext(
app: NativeContextApp(
name: html.window.navigator.appName,
version: html.window.navigator.appVersion,
namespace: html.window.navigator.appCodeName),
userAgent: html.window.navigator.userAgent,
locale: html.window.navigator.language,
screen: html.window.screen != null
? NativeContextScreen(
height: html.window.screen?.height,
width: html.window.screen?.width)
: null));
Future<NativeContext> getContext({bool collectDeviceId = false}) async =>
NativeContext(
app: NativeContextApp(
name: web.window.navigator.appName,
version: web.window.navigator.appVersion,
namespace: web.window.navigator.appCodeName,
),
userAgent: web.window.navigator.userAgent,
locale: web.window.navigator.language,
screen: NativeContextScreen(
height: web.window.screen.height,
width: web.window.screen.width,
),
);
}

class AnalyticsWeb {
Expand Down
71 changes: 28 additions & 43 deletions packages/core/lib/utils/store/web.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;

import 'package:segment_analytics/utils/store/store.dart';
import 'package:web/web.dart' as web;

class StoreImpl implements Store {
html.Storage get localStorage => html.window.localStorage;
web.Storage get localStorage => web.window.localStorage;

@override
Future<Map<String, dynamic>?> getPersisted(String key) {
Expand All @@ -17,52 +17,45 @@ class StoreImpl implements Store {

@override
Future setPersisted(String key, Map<String, dynamic> value) {
return _writeToStorage(key, value);
_writeToStorage(key, value);
return Future.value();
}

String _getFileName(String fileKey) {
return "analytics-flutter-$fileKey.json";
}

Future<void> _writeToStorage(
String fileKey, Map<String, dynamic> data) async {
localStorage.update(
void _writeToStorage(String fileKey, Map<String, dynamic> data) {
localStorage.setItem(
_getFileName(fileKey),
(val) => json.encode(data),
ifAbsent: () => json.encode(data),
json.encode(data),
);
}

Future<Map<String, dynamic>?> _readFromStorage(String fileKey) async {
final fileName = _getFileName(fileKey);
MapEntry<String, String>? data;
final data = localStorage.getItem(fileName);
String anonymousId;

try {
data = localStorage.entries.firstWhere((i) => i.key == fileName);
} on StateError {
data = null;
}

if(fileKey == "userInfo") {
anonymousId = getExistingAnonymousId(data);
if(data != null) {
final jsonDecoded = json.decode(data.value);
if(anonymousId.isNotEmpty){
if (fileKey == "userInfo") {
anonymousId = getExistingAnonymousId();
if (data != null) {
final jsonDecoded = json.decode(data);
if (anonymousId.isNotEmpty) {
jsonDecoded["anonymousId"] = anonymousId;
return jsonDecoded as Map<String, dynamic>;
}
} else if(anonymousId.isNotEmpty){
final json = {"anonymousId": anonymousId };
} else if (anonymousId.isNotEmpty) {
final json = {"anonymousId": anonymousId};
return json;
}
}

if (data != null) {
if (data.value == "{}") {
if (data == "{}") {
return null; // Prefer null to empty map, because we'll want to initialise a valid empty value.
}
return json.decode(data.value) as Map<String, dynamic>;
return json.decode(data) as Map<String, dynamic>;
} else {
return null;
}
Expand All @@ -71,28 +64,20 @@ class StoreImpl implements Store {
@override
void dispose() {}

String getExistingAnonymousId(MapEntry<String, String>? data) {
String anonymousId;
try {
final entry = localStorage.entries.firstWhere(
(i) => i.key == "ajs_anonymous_id",
);
anonymousId = entry.value;
} on StateError {
anonymousId = '';
}
String getExistingAnonymousId() {
var anonymousId = localStorage.getItem("ajs_anonymous_id");

if (anonymousId.isEmpty) {
final cookies = html.document.cookie?.split(";");
if (cookies != null && cookies.isNotEmpty) {
for (var cookie in cookies) {
final cookieParts = cookie.split("=");
if (cookieParts[0].trim() == "ajs_anonymous_id") {
anonymousId = cookieParts[1];
}
if (anonymousId?.isEmpty ?? true) {
final cookies = web.document.cookie.split(";");
if (cookies.isNotEmpty) {
for (var cookie in cookies) {
final cookieParts = cookie.split("=");
if (cookieParts[0].trim() == "ajs_anonymous_id") {
anonymousId = cookieParts[1];
}
}
}
return anonymousId.isEmpty ? '' : anonymousId;
}
return anonymousId ?? '';
}
}
1 change: 1 addition & 0 deletions packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
path_provider: ^2.0.12
flutter_fgbg: ^0.3.0
shared_preferences: ^2.2.2
web: ^0.5.1

dev_dependencies:
build_runner: ^2.3.3
Expand Down