Hello Copilot
The examples below illustrate how to use the main Copilot.cx SDK components.
Chapter 2 is a walkthrough of a single Copilot.cx based flow - how to register an authenticated session for a new user created in Copilot, how to report the analytic events associated with the user, and how to associate a hardware Thing with this User.
💡 Make sure you followed the Importing Copilot.cx SDK for Android and iOS
Authentication - Register user
User registration(signup) is under Auth component, and the auto-completion guides you through the command invocation.
Make sure you call build()
and then execute()
on the requested command.
⭐ More details about the Auth commands can be found here
Copilot.instance
.manage
.sphere
.auth
.signup()
.allowCopilotUserAnalysis(true)
.with(email: "ck@dailyplanet.com", password: "Superman1234", firstname: "Clark", lastname: "Kent")
.build()
.execute { [weak self] (response) in
switch response {
case .success:
// User has been successfully registered
case .failure(error: let error):
// Failed to register user, handle error
}
}
Copilot.getInstance()
.Manage
.Sphere
.Auth
.signup()
.withCopilotAnalysisConsent(true)
.withEmailPassword("ck@dailyplanet.com", "Superman1234", "Clark", "Kent")
.build()
.execute(new RequestListener<Void, SignupError>() {
@Override
public void success(Void response) {
// User has been successfully registered
}
@Override
public void error(SignupError error) {
// Failed to register user, handle error
}
});
Copilot.getInstance()
.Manage
.Sphere
.Auth
.signup()
.withCopilotAnalysisConsent(true)
.withEmailPassword("ck@dailyplanet.com", "Superman1234", "Clark", "Kent")
.build()
.execute(object: RequestListener<Void?, SignupError> {
override fun success(response: Void?) {
// User has been successfully registered
}
override fun error(error: SignupError?) {
// Failed to register user, handle error
}
})
Track application behavior - Report
Reporting every event in your application allows Copilot.cx platform to analyse and engage correctly your users.
⭐ More details about Report component can be found here.
The Copilot.cx SDK already contains a set of predefined analytics events, simplifying the way you report events. Below an example of a signup event, which you should report when receiving a successful response upon registration.
Below you can see an example how to report the Signup succeeded analytics event
Copilot.instance.report.log(event: SignupAnalyticsEvent())
Copilot.getInstance().Report.logEvent(new SignupAnalyticsEvent());
Copilot.getInstance().Report.logEvent(SignupAnalyticsEvent())
Once you authenticated
Know your user - FetchMe
When fetching the user, you will receive an instance of UserMe, containing the UserDetails
and the AccountStatus
.
⭐ More details about User component can be found here.
Below examples for iOS and Android:
Copilot.instance
.manage
.sphere
.user
.fetchMe()
.build()
.execute { (response) in
switch response {
.success(let userMe):
// fetchMe has been fetched with success
.failure(let error):
// Failed to fetch fetchMe, handle error
}
}
Copilot.getInstance()
.Manage
.Sphere
.User
.fetchMe()
.build()
.execute(new RequestListener<UserMeModel, FetchMeError>() {
@Override
public void success(UserMeModel response) {
// fetchMe has been fetched with success
}
@Override
public void error(FetchMeError error) {
// Failed to fetch fetchMe, handle error
}
});
Copilot.getInstance()
.Manage
.Sphere
.User
.fetchMe()
.build()
.execute(object : RequestListener<UserMeModel, FetchMeError> {
override fun success(response: UserMeModel) {
// fetchMe has been fetched with success
}
override fun error(error: FetchMeError) {
// Failed to fetch fetchMe, handle error
}
})
Know your user’s connected devices - Associate Thing
Once you have registered a user, you can start adding connected devices to their account.
You will find associateThing
under the Thing API.
⭐ More about the Thing component can be found here.
Below you will find an example of how to add a connected device to your user.
Copilot.instance
.manage
.sphere
.thing
.associateThing()
.with(physicalID: "PhysicalId", firmware: "Firmware", model: "Model")
.build()
.execute { (response) in
switch response {
case .success(let thing):
// Thing has been associated with success
case .failure(error: let associationError):
// Failed to associate the thing, handle the error
}
}
Copilot.getInstance()
.Manage
.Sphere
.Thing
.associateThing("physicalId", "firmware", "model")
.build()
.execute(new RequestListener<ThingModel, AssociateThingError>() {
@Override
public void success(ThingModel response) {
// Thing has been associated with success
}
@Override
public void error(AssociateThingError error) {
// Failed to associate the thing, handle the error
}
});
Copilot.getInstance()
.Manage
.Sphere
.Thing
.associateThing("physicalId", "firmware", "model")
.build()
.execute(object : RequestListener<ThingModel, AssociateThingError> {
override fun success(response: ThingModel) {
// Thing has been associated with success
}
override fun error(error: AssociateThingError) {
// Failed to associate the thing, handle the error
}
})