getExistingAnonymousId method

String getExistingAnonymousId()

Implementation

String getExistingAnonymousId() {
  var anonymousId = localStorage.getItem("ajs_anonymous_id");

  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];
        }
      }
    }
  }

  if (anonymousId != null) {
    try {
      final decoded = json.decode(anonymousId);
      if (decoded is String) {
        anonymousId = decoded;
      }
    } catch (_) {}
  }

  return anonymousId ?? '';
}