Skip to content

Commit

Permalink
feat(Amplitude): update amplitude SDK (#1413)
Browse files Browse the repository at this point in the history
* feat(Amplitude): update amplitude SDK (#1228)

* feat(Amplitude): update amplitude SDK

* feat: update logEvent to track

* update mobile and web sdk common fields

* update default value of trackNewCampaigns

* update default eventUploadPeriodMillis to 1000

---------

Co-authored-by: George Bardis <[email protected]>

* feat(analytics-js-integrations): add test of IE11

---------

Co-authored-by: Sudip Paul <[email protected]>
Co-authored-by: ItsSudip <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 94f4b2d commit 6636770
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable class-methods-use-this */
import { NAME } from '@rudderstack/analytics-js-common/constants/integrations/Amplitude/constants';
import Logger from '../../utils/logger';
import { type } from '../../utils/utils';
import { loadNativeSdk } from './nativeSdkLoader';
import { getTraitsToSetOnce, getTraitsToIncrement, getDestinationOptions } from './utils';

Expand All @@ -19,15 +18,10 @@ class Amplitude {
this.trackAllPages = config.trackAllPages || false;
this.trackNamedPages = config.trackNamedPages || false;
this.trackCategorizedPages = config.trackCategorizedPages || false;
this.trackUtmProperties = config.trackUtmProperties || false;
this.trackReferrer = config.trackReferrer || false;
this.batchEvents = config.batchEvents || false;
this.eventUploadThreshold = +config.eventUploadThreshold || 30;
this.eventUploadPeriodMillis = +config.eventUploadPeriodMillis || 30000;
this.forceHttps = config.forceHttps || false;
this.trackGclid = config.trackGclid || false;
this.saveParamsReferrerOncePerSession = config.saveParamsReferrerOncePerSession || false;
this.deviceIdFromUrlParam = config.deviceIdFromUrlParam || false;
this.attribution = config.attribution || false;
this.flushQueueSize = config.eventUploadThreshold || 30;
this.flushIntervalMillis = +config.eventUploadPeriodMillis || 1000;
this.trackNewCampaigns = config.trackNewCampaigns || true;
this.trackRevenuePerProduct = config.trackRevenuePerProduct || false;
this.preferAnonymousIdForDeviceId = config.preferAnonymousIdForDeviceId || false;
this.traitsToSetOnce = getTraitsToSetOnce(config);
Expand All @@ -49,32 +43,20 @@ class Amplitude {
}

const initOptions = {
includeUtm: this.trackUtmProperties,
batchEvents: this.batchEvents,
eventUploadThreshold: this.eventUploadThreshold,
eventUploadPeriodMillis: this.eventUploadPeriodMillis,
forceHttps: this.forceHttps,
includeGclid: this.trackGclid,
includeReferrer: this.trackReferrer,
saveParamsReferrerOncePerSession: this.saveParamsReferrerOncePerSession,
deviceIdFromUrlParam: this.deviceIdFromUrlParam,
unsetParamsReferrerOnNewSession: this.unsetParamsReferrerOnNewSession,
deviceId:
this.preferAnonymousIdForDeviceId && this.analytics && this.analytics.getAnonymousId(),
attribution: { disabled: this.attribution, trackNewCampaigns: !this.trackNewCampaigns },
flushQueueSize: this.flushQueueSize,
flushIntervalMillis: this.flushIntervalMillis,
appVersion: this.versionName,
};
window.amplitude.getInstance().init(this.apiKey, null, initOptions);
if (this.versionName) {
window.amplitude.getInstance().setVersionName(this.versionName);
if (
navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > -1
) {
initOptions.transport = 'xhr';
}
}

isLoaded() {
logger.debug('in Amplitude isLoaded');
return !!window?.amplitude?.getInstance()?.options;
}

isReady() {
return !!window?.amplitude?.getInstance()?.options;
if (this.preferAnonymousIdForDeviceId && this.analytics)
initOptions.deviceId = this.analytics.getAnonymousId();
window.amplitude.init(this.apiKey, null, initOptions);
}

identify(rudderElement) {
Expand All @@ -87,15 +69,14 @@ class Amplitude {
const { userId } = rudderElement.message;

if (userId) {
window.amplitude.getInstance().setUserId(userId);
window.amplitude.setUserId(userId);
}

if (traits) {
const amplitudeIdentify = new window.amplitude.Identify();
Object.keys(traits).forEach(trait => {
const shouldIncrement = this.traitsToIncrement.includes(trait);
const shouldSetOnce = this.traitsToSetOnce.includes(trait);

if (shouldIncrement) {
amplitudeIdentify.add(trait, traits[trait]);
}
Expand Down Expand Up @@ -126,7 +107,7 @@ class Amplitude {

// For track products once, we will send the products in a single call.
if (this.trackProductsOnce) {
if (products && type(products) === 'array') {
if (products && Array.isArray(products)) {
// track all the products in a single event.
const allProducts = [];

Expand Down Expand Up @@ -154,7 +135,7 @@ class Amplitude {
return;
}

if (products && type(products) === 'array') {
if (products && Array.isArray(products)) {
// track events iterating over product array individually.

// Log the actuall event without products array. We will subsequently track each product with 'Product Purchased' event.
Expand Down Expand Up @@ -200,7 +181,7 @@ class Amplitude {
logEventAndCorrespondingRevenue(rudderMessage, dontTrackRevenue) {
const { properties, event } = rudderMessage;

window.amplitude.getInstance().logEvent(event, properties);
window.amplitude.track(event, properties);
if (properties.revenue && !dontTrackRevenue) {
this.trackRevenue(rudderMessage);
}
Expand All @@ -223,23 +204,23 @@ class Amplitude {
// all pages
if (this.trackAllPages) {
const event = 'Loaded a page';
window.amplitude.getInstance().logEvent(event, properties);
window.amplitude.track(event, properties);
}

// categorized pages
if (category && this.trackCategorizedPages) {
let event;
if (!useNewPageEventNameFormat) event = `Viewed page ${category}`;
else event = `Viewed ${category} Page`;
window.amplitude.getInstance().logEvent(event, properties);
window.amplitude.track(event, properties);
}

// named pages
if (name && this.trackNamedPages) {
let event;
if (!useNewPageEventNameFormat) event = `Viewed page ${name}`;
else event = `Viewed ${name} Page`;
window.amplitude.getInstance().logEvent(event, properties);
window.amplitude.track(event, properties);
}
}

Expand All @@ -261,10 +242,10 @@ class Amplitude {
}

if (groupType && groupValue) {
window.amplitude.getInstance().setGroup(groupTypeTrait, groupValueTrait);
window.amplitude.setGroup(groupTypeTrait, groupValueTrait);
} else if (groupId) {
// Similar as segment but not sure whether we need it as our cloud mode supports only the above if block
window.amplitude.getInstance().setGroup('[Rudderstack] Group', groupId);
window.amplitude.setGroup('[Rudderstack] Group', groupId);
}

// https://developers.amplitude.com/docs/setting-user-properties#setting-group-properties
Expand All @@ -274,7 +255,7 @@ class Amplitude {
setDeviceId(rudderElement) {
const { anonymousId } = rudderElement.message;
if (this.preferAnonymousIdForDeviceId && anonymousId) {
window.amplitude.getInstance().setDeviceId(anonymousId);
window.amplitude.setDeviceId(anonymousId);
}
}

Expand Down Expand Up @@ -334,7 +315,7 @@ class Amplitude {
delete amplitudeRevenue._properties.productId;
delete amplitudeRevenue._properties.quantity;
}
window.amplitude.getInstance().logRevenueV2(amplitudeRevenue);
window.amplitude.revenue(amplitudeRevenue);
}

getProductAttributes(product) {
Expand All @@ -347,6 +328,15 @@ class Amplitude {
category: product.category,
};
}

isLoaded() {
logger.debug('in Amplitude isLoaded');
return !!(window.amplitude && window.amplitude.getDeviceId());
}

isReady() {
return !!(window.amplitude && window.amplitude.getDeviceId());
}
}

export default Amplitude;
Loading

0 comments on commit 6636770

Please sign in to comment.