Skip to content

Commit

Permalink
feat(Amplitude): update amplitude SDK (#1228)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ItsSudip and bardisg authored Sep 28, 2023
1 parent 18918ab commit c04ef53
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 149 deletions.
78 changes: 31 additions & 47 deletions src/integrations/Amplitude/browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable class-methods-use-this */
import Logger from '../../utils/logger';
import { type } from '../../utils/utils';
import { NAME } from './constants';

import { loadNativeSdk } from './nativeSdkLoader';
Expand All @@ -20,15 +19,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 @@ -50,32 +44,14 @@ 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);
}
}

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 @@ -88,15 +64,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 @@ -127,7 +102,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 @@ -155,7 +130,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 @@ -201,7 +176,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 +198,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 +236,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 +249,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 +309,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 +322,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 c04ef53

Please sign in to comment.