sendTrackEventRequest method
Implementation
Future<Map<String, dynamic>> sendTrackEventRequest(
String screen, Map<String, dynamic> event) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
int intMax = 4294967296;
var selectedSpotCheckID = intMax;
if (customEventsSpotChecks.isNotEmpty) {
for (Map<String, dynamic> spotCheck in customEventsSpotChecks) {
Map<String, dynamic> checks =
spotCheck["checks"] ?? spotCheck["checkCondition"];
if (checks.isNotEmpty) {
Map<String, dynamic> customEvent = checks["customEvent"] ?? {};
if (customEvent.isNotEmpty && event.keys.contains(customEvent["eventName"])) {
selectedSpotCheckID =
spotCheck["id"] ?? spotCheck["spotCheckId"] ?? intMax;
Map<String, dynamic> payloadUserDetails = Map.from(userDetails);
if (selectedSpotCheckID != intMax) {
if (payloadUserDetails["email"] == null &&
payloadUserDetails["uuid"] == null &&
payloadUserDetails["mobile"] == null) {
String? uuid = prefs.getString("SurveySparrowUUID");
if (uuid != null && uuid.isNotEmpty) {
payloadUserDetails["uuid"] = "uuid";
}
}
Map<String, dynamic> payload = {
"url": screen,
"variables": variables,
"userDetails": payloadUserDetails,
"visitor": {
"deviceType": "MOBILE",
"operatingSystem": Platform.operatingSystem,
"screenResolution": {
"height": screenHeight,
"width": screenWidth
},
"currentDate": DateTime.now().toIso8601String(),
"timezone": DateTime.now().timeZoneName,
},
"spotCheckId": selectedSpotCheckID,
"eventTrigger": {
"customEvent": event,
},
"traceId": traceId.value,
"customProperties": customProperties
};
final Uri url = Uri.parse(
'https://$domainName/api/internal/spotcheck/widget/$targetToken/eventTrigger')
.replace(queryParameters: {"isSpotCheck": "true"});
try {
final http.Response response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(payload),
);
if (response.statusCode == 200) {
final Map<String, dynamic>? responseJson =
jsonDecode(response.body);
if (responseJson?["show"] != null) {
bool show = responseJson?["show"];
setAppearance(responseJson!, screen);
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..loadRequest(Uri.parse(spotcheckURL.value))
..setNavigationDelegate(NavigationDelegate(
onPageFinished: (url) {
isLoading.value = false;
},
))
..addJavaScriptChannel("flutterSpotCheckData",
onMessageReceived: (JavaScriptMessage response) {
try {
var jsonResponse = json.decode(response.message);
log(jsonResponse.toString());
if (jsonResponse['type'] == "spotCheckData") {
var height = jsonResponse['data']
['currentQuestionSize']['height'];
currentQuestionHeight.value = height;
} else if (jsonResponse['type'] ==
"surveyCompleted") {
end();
}
} catch (e) {
log("Error decoding JSON: $e");
}
});
_isSpotPassed.value = true;
if (show) {
log("Success: Spots or Checks or Visitor or Reccurence Condition Passed");
return {"valid": true};
} else {
log("Error: Spots or Checks or Visitor or Reccurence Condition Failed");
return {"valid": false};
}
} else {
log("Error: Show not Received");
}
if (!_isSpotPassed.value) {
if (responseJson?["eventShow"]) {
if (responseJson?["checkCondition"] != null) {
Map<String, dynamic> checkCondition =
responseJson?["checkCondition"];
if (checkCondition["afterDelay"] != null) {
afterDelay.value = checkCondition["afterDelay"];
}
if (checkCondition["customEvent"] != null) {
var delay =
checkCondition["customEvent"]?["delayInSeconds"];
afterDelay.value = delay ?? 0;
}
}
setAppearance(responseJson!, screen);
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..loadRequest(Uri.parse(spotcheckURL.value))
..setNavigationDelegate(NavigationDelegate(
onPageFinished: (url) {
isLoading.value = false;
},
))
..addJavaScriptChannel("flutterSpotCheckData",
onMessageReceived: (JavaScriptMessage response) {
try {
var jsonResponse = json.decode(response.message);
log(jsonResponse.toString());
if (jsonResponse['type'] == "spotCheckData") {
var height = jsonResponse['data']
['currentQuestionSize']['height'];
currentQuestionHeight.value = height;
} else if (jsonResponse['type'] ==
"surveyCompleted") {
end();
}
} catch (e) {
log("Error decoding JSON: $e");
}
});
log("Success: Checks Condition Passed");
return {"valid": true};
}
}
} else {
log('Error: ${response.statusCode}');
return {"valid": false};
}
} catch (error) {
log('Error: $error');
return {"valid": false};
}
}
}
}
}
return {"valid": true};
} else {
return {"valid": false};
}
}