Skip to main content

Ionic Angular

Latest Version: 2.1.0

Below are the various processes involved in setting up Spotcheck in Ionic Angular

Installation​

To install the SurveySparrowSdk, make sure you are in the root folder of your Ionic Angular project in the terminal and run the following command.

# NPM Installation
npm install surveysparrow-ionic-plugin

# YARN Installation
yarn add surveysparrow-ionic-plugin

The plugin needs Angular 16 or newer with @angular/router, and Capacitor 7 or newer. It works on web, Android and iOS from the same code.

  • The SurveySparrowSdk requires Internet access to fetch surveys and submit answers

  • It also requires camera permission to attend the photo capture Questions.

  • So, add the following permissions to the AndroidManifest.xml file for Android

 <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
  • Also add the camera permissions in info.plist for IoS
<key>NSCameraUsageDescription</key>
<string>Your-Description</string>

Initialization​

Initialize spotchecks in root component's (eg app.component.ts) constructor. Add this once in the entire app.

Anonymous users​

If you wish not to keep track of users' data, you can follow the below syntax for initialization.

  constructor() {
try {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
userDetails: {},
variables: {
sparrowLang: 'lang-code',
},
customProperties: {},
});
} catch (error) {
console.error('Failed to initialize SpotChecks:', error);
}
}

Known Users​

If you wish to keep track of users' data and perform conditional Survey triggers, you can follow the below syntax for initialization.

  constructor() {
try {
initializeSpotChecks({
domainName: 'your-domain-name',
targetToken: 'your-target-token',
userDetails: {
email:"<user_email>",
mobile:"<user_mobile>",
uuid: '<uuid_value>' // Optional
},
variables: {
sparrowLang: "ta" // Eg: ta, en,
<api_identifier_of_variable>:"<variable_value>",
},
customProperties: {
<custom_property_name>: '<custom_property_value>'
},
});
} catch (error) {
console.error('Failed to initialize SpotChecks:', error);
}
}

Add Spotcheck tag in last line of the html in the root component's html eg(app.component.html)

  <ion-app>
<ion-router-outlet></ion-router-outlet>
</ion-app>

<SpotCheck />

When the user navigates with the Angular Router, an open survey closes automatically. If your app changes screens another way (for example without the Angular Router), call closeSpotChecks() when the screen changes.

import { closeSpotChecks } from 'surveysparrow-ionic-plugin/angular-ui';

closeSpotChecks();

Screen Track​

It provides the ability to keep track of screens the users are visiting and to enable the survey trigger on that screen. Also user can able to pass the dynamic variables in the screen tracking function. Call the trackScreen function each time the screen is shown. In Ionic pages use ionViewDidEnter: Ionic keeps pages in memory, so ngOnInit does not run again when the user comes back to a page. In other Angular components, use ngOnInit.

Syntax:​

  ionViewDidEnter() {
this.trackHomeScreen();
}

private async trackHomeScreen() {
try {
await trackScreen({
screen: 'HomeScreen',
options: {
userDetails: {
email: '<user_email>',
mobile: '<user_mobile>',
uuid: '<uuid_value>' // Optional
} // optional,
variables: {
sparrowLang: 'ta', // Language code (e.g., 'ta', 'en')
<api_identifier_of_variable>: '<variable_value>',
} // optional,
customProperties: {
<custom_property_name>: '<custom_property_value>'
} // optional
},
});
} catch (error) {
console.log('Error tracking home screen:', error);
}
}

Example:​

If a survey needs to be triggered on the payment page, the name of the ScreenName should be specified in the TrackScreen function. Also we can pass the dynamic variables as shown below.

  ionViewDidEnter() {
this.trackHomeScreen();
}

private async trackHomeScreen() {
try {
await trackScreen({
screen: 'HomeScreen',
options: {
variables: {
sparrowLang: 'en',
},
userDetails: {
email: 'user@example.com',
},
customProperties: {
userId: 10000234
}
},
});
} catch (error) {
console.log('Error tracking home screen:', error);
}
}

Event Track​

It provides the ability to keep track of events which when triggered, will enable the survey to be popped. trackEvent function can be called in any events of a screen.

Syntax:​

  try {
await trackEvent({
screen: '<your-screen-name>',
event: {
<your-events-or-functions>: {
<your-event-property>: <your-event-property-value>,
},
},
});
} catch (error) {
console.error('Error tracking home event:', JSON.stringify(error));
}

Example:​

If a survey needs to be triggered when the user completes a payment, then the TrackEvent function should be called with the respective ScreenName and optional custom properties.

  try {
await trackEvent({
screen: 'PaymentScreen',
event: {
paymentCompletedEvent: {
paymentStatus: "COMPLETED",
paymentDoneOn: Date.now().toString(),
email: 'user@example.com'
},
},
});
} catch (error) {
console.error('Error tracking home event:', JSON.stringify(error));
}
Make sure to have the eventName configured in the Checks section of the configure panel

Callbacks (Optional)​

Callbacks are used to listen to events that are part of the SpotChecks. All callback functions are optional — you can implement only the ones you need. The available callbacks include:

  • Survey Response - triggers when the user submits a response.

  • Survey Loaded - triggers when the SpotChecks is loaded on the user's page.

  • Survey Close - triggers when the SpotChecks is closed by the user.

  • Partial Submission - triggers when the PartialSubmission is enabled and received.

Syntax:​

// Step 1: Add callbacks to the SpotChecks Initialization
initializeSpotChecks({
//...
listener: listener
});

// Step 2: Listen to the SpotChecks events
const listener = {
onSurveyLoaded: async (response: Record<string, any>) => {},

onSurveyResponse: async (response: Record<string, any>) => {},

onCloseButtonTap: async () => {},

onPartialSubmission: async (response: Record<string, any>) => {}
};

addSpotCheckListener from earlier versions (surveyLoadStarted, surveyCompleted, surveyDismissed) still works.