Skip to content

AppsFlyer Analytics Module

The AppsFlyer Analytics module logs in-app events through the native AppsFlyer SDK. The SDK itself (dev key, app ID) is set up as part of the app configuration.

Available from @shopgate/native-modules 1.1.1.

import { AppsFlyerAnalytics } from '@shopgate/native-modules';

The app only sends events to AppsFlyer after the user has agreed to tracking. Before that, logEvent resolves without sending anything and without an error.

Depending on the app configuration, tracking is enabled in one of these ways:

  • At app start: on iOS when the user accepts the system tracking prompt (App Tracking Transparency), on Android right away.
  • By your website: when the website passes the user’s consent decision to the app (see below).
  • Always: if the app is configured to track regardless of consent.

Ask Shopgate which of these applies to your app.

Section titled “Pass the consent decision from your website”
window.SGJavascriptBridge?.dispatchCommandsForVersion([
{
c: 'analyticsSetConsent',
p: { serial: Date.now(), statistics: true, comfort: true },
},
], '9.0');
Name Type Required Description
serial number yes Any unique number.
statistics boolean yes true enables AppsFlyer tracking.
comfort boolean no Consent for comfort tracking, used by other native trackers.

The command applies to all native trackers configured for the app, not only AppsFlyer. Sending statistics: false does not stop tracking that was already enabled while the app is running.

Logs an event through the native AppsFlyer SDK.

await AppsFlyerAnalytics.logEvent({
name: 'af_add_to_cart',
eventParams: {
af_content_id: 'product-123',
af_currency: 'USD',
af_price: 24.99,
},
});
Name Type Required Description
name string yes AppsFlyer event name. Use the predefined names (af_purchase, af_add_to_cart, af_content_view, …) where they fit, or your own name.
eventParams object no Event parameters with string, number, boolean, or null values.

Returns Promise<void>. The promise rejects if the installed app version does not support the module, so wrap calls in try/catch.

import { isAvailable, AppsFlyerAnalytics } from '@shopgate/native-modules';
export async function trackPurchase(order) {
if (!isAvailable()) return;
try {
await AppsFlyerAnalytics.logEvent({
name: 'af_purchase',
eventParams: {
af_revenue: order.total,
af_currency: order.currency,
af_order_id: order.id,
af_quantity: order.itemCount,
},
});
} catch (error) {
// App version without AppsFlyer support -> ignore
}
}

The app reports links that open it through the Linking module, but does not pass them on to AppsFlyer. To see these opens in AppsFlyer, log your own event with the campaign parameters of the link.

On a cold start the link usually arrives before the user has answered the consent banner, so an event logged right away would be dropped. Store the link first and log the event once tracking is allowed:

import { isAvailable, AppsFlyerAnalytics } from '@shopgate/native-modules';
const STORAGE_KEY = 'pendingAppLinkOpen';
// Call in your universalLinkOpened handler, before navigating to the link
export function rememberLinkOpen(link) {
try { sessionStorage.setItem(STORAGE_KEY, link); } catch {}
}
// Call on page load and right after the user has given consent
export async function logPendingLinkOpen() {
if (!isAvailable()) return;
let link;
try { link = sessionStorage.getItem(STORAGE_KEY); } catch {}
if (!link) return;
const params = new URL(link).searchParams;
try {
await AppsFlyerAnalytics.logEvent({
name: 'app_link_opened',
eventParams: {
link,
utm_source: params.get('utm_source'),
utm_medium: params.get('utm_medium'),
utm_campaign: params.get('utm_campaign'),
},
});
sessionStorage.removeItem(STORAGE_KEY);
} catch (error) {
// App version without AppsFlyer support -> ignore
}
}

Only call logPendingLinkOpen() once tracking is allowed. Otherwise the event is dropped and the stored link is removed anyway.