track static method

void track(
  1. String trackName,
  2. String serverUrl,
  3. String userWriteKey,
  4. TrackBulider properties,
  5. OtherID otherid,
)

Implementation

static void track(String trackName, String serverUrl, String userWriteKey, TrackBulider properties, OtherID otherid) async {
  String? _urlWriteKey;
  try {
    TrackModel trackModel = TrackModel();
    trackModel.activityName = trackName;
    trackModel.properties = properties.properties;
    trackModel.otherid = otherid.trackId;
    TrackModel? fMain = await trackModel;

    //getting device details
    String? deviceId;
    String? deviceType;
    String? deviceManufacture;
    String? deviceModel;
    String? versionName;
    //getting app information
    String? appName;
    String? appVersion;
    String? appBuild;
    //google adertising id its bool value
    String? googleAdvertisingID;
    dynamic? googleAdvertisingBool;
    //network ip
    String? networkIP;
    //phone height width
    String? appScreenHeightWidth;
    String? appScreenHeightHeight;
    //json reseived send timestemp
    String? appDataReceivedAt;
    String? appDataSenddAt;
    String? appOriginalTimetemp;
    String? appTimestemp;
    //write key
    String? writekey;

    try {
      deviceId = await LemniskPlateform.getDeviceId;
      deviceType = await LemniskPlateform.getPlatFormDeviceType;
      deviceModel = await LemniskPlateform.getDeviceModel;
      deviceManufacture = await LemniskPlateform.getManufactureModel;
      versionName = await LemniskPlateform.getOSVersion;
      appName = await LemniskPlateform.getAppName;
      appVersion = await LemniskPlateform.getAppVersion;
      appBuild = await LemniskPlateform.getBuildNumber;
      googleAdvertisingID = await LemniskPlateform.getGoogleadvertisingID;
      googleAdvertisingBool = await LemniskPlateform.getGoogleadvertisingIDBoolValue;
      networkIP = await LemniskPlateform.getNetworkIP;
      writekey = await LemniskPlateform.getWriteKey;

      appScreenHeightHeight = await LemniskPlateform.getScreenHeight;
      appScreenHeightWidth = await LemniskPlateform.getScreenWidth;

      appDataReceivedAt = await LemniskPlateform.getReceivedAt;
      appDataSenddAt = await LemniskPlateform.getSendAt;

      appOriginalTimetemp = await LemniskPlateform.getOriginalTimetemp;
      appTimestemp = await LemniskPlateform.getTimestemp;
    } on PlatformException {
      deviceId = 'Failed to get deviceId.';
    }

    //after received all data create josn payload...now we can hit the server  //context nested map prepare
    if (userWriteKey.isEmpty) {
      _urlWriteKey = writekey;
    } else {
      _urlWriteKey = userWriteKey;
    }

    Map<dynamic, dynamic> lib = {
      "library": {
        "name": "Flutter SDK",
        "version": "0.03",
      },
      "app": {
        "name": appName,
        "version": appVersion,
        "build": appBuild,
      },
      "device": {
        "id": deviceId,
        "advertisingId": googleAdvertisingID,
        "adTrackingEnabled": googleAdvertisingBool,
        "manufacturer": deviceManufacture,
        "model": deviceModel,
        "type": deviceType,
        "token": "ff15bc0c20c4aa6cd50854ff165fd265c838e5405bfeb9571066395b8c9da449",
      },
      "screen": {
        "width": appScreenHeightWidth,
        "height": appScreenHeightHeight,
        "density": 2,
      },
      "userAgent": {
        "osType": deviceType,
        "osVersion": versionName,
      },
      "ip": networkIP
    };

    Map map = {
      "id": getRandomString(20),
      "userId": fMain.otherid,
      "otherIds": fMain.otherid,
      "event": trackName,
      "messageId": "ajs-f8ca1e4de5024d9430b3928bd8ac6b96",
      "properties": fMain.properties,
      "receivedAt": appDataReceivedAt,
      "sentAt": appDataSenddAt,
      "timestamp": appTimestemp,
      "type": "track",
      "originalTimestamp": appOriginalTimetemp,
      "writeKey": _urlWriteKey,
    };
    map.addAll({"context": lib});

    String url;
    //we can hit on server....
    if (serverUrl.isEmpty) {
      url = "https://tpl.lemnisk.co/analyze/analyze.php";
    } else {
      url = serverUrl;
    }
    print('trackevent' + url);
    Uri uri = Uri.parse(url);
    var response = await http.post(uri, body: JsonEncoder().convert(map));
    if (response.statusCode == 200) {
      final String responseString = response.body;
      print('event track success=====   $responseString');
    } else {
      final String responseString = response.body;
      print('event track falied=====   $responseString');
    }
  } on PlatformException {
    print('some exception occoured');
  }
}