blob: ad4cfd74194624341aac98584f49ac8142aae90f [file] [log] [blame]
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. 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:convert';
import 'package:http/http.dart' as http;
import 'constants.dart';
class GAClient {
final String measurementId;
final String apiSecret;
final String postUrl;
final http.Client _client;
GAClient({
required this.measurementId,
required this.apiSecret,
}) : postUrl =
'$kAnalyticsUrl?measurement_id=$measurementId&api_secret=$apiSecret',
_client = http.Client();
/// Closes the http client's connection to prevent lingering requests
void close() => _client.close();
/// Receive the payload in Map form and parse
/// into JSON to send to GA
Future<http.Response> sendData(Map<String, Object?> body) {
return _client.post(
Uri.parse(postUrl),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(body),
);
}
}