Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"mocha/no-exclusive-tests": "error"
},
"globals": {
"BUILD_COMPAT_REACT_NATIVE": "readonly",
"BUILD_COMPAT_LOCAL_STORAGE": "readonly",
"BUILD_COMPAT_SNIPPET": "readonly",
"BUILD_COMPAT_2_0": "readonly",
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/Bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ labels: 'bug'
<!--- (Not obligatory) Suggest a fix/reason -->

## Steps to Reproduce
<!--- Please provide a clear sequence of steps to reproduce this bug -->
<!--- Please provide a clear sequence of steps to reproduce this bug -->
<!--- Include code and images, if relevant -->
1.
2.
Expand All @@ -26,4 +26,4 @@ labels: 'bug'
## Environment
- JS SDK Version: <!--- E.g. 7.1.0 -->
- Installation Method: <!-- I.e. NPM/Yarn or <script> import -->
- Browser and Version or React Native and Device: <!-- E.g. Chrome 84, React Native@0.63 iPhone SE-->
- Browser and Version: <!-- E.g. Chrome 84-->
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ amplitude.js
amplitude.esm.js
amplitude.min.js
amplitude-snippet.min.js
amplitude.native.js
amplitude-segment-snippet.min.js
.watchmanconfig
package-lock.json
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ README.md: $(SNIPPET_OUT) version
# Target for `amplitude.js` file.
#

$(OUT): node_modules $(SRC) package.json rollup.config.js rollup.min.js rollup.native.js rollup.esm.js rollup.umd.js rollup.umd.min.js
$(OUT): node_modules $(SRC) package.json rollup.config.js rollup.min.js rollup.esm.js rollup.umd.js rollup.umd.min.js
@NODE_ENV=production $(ROLLUP) --config rollup.config.js # is the snippet build config
@NODE_ENV=production $(ROLLUP) --config rollup.esm.js # does not concat dependencies, only has module and dependencies
@NODE_ENV=production $(ROLLUP) --config rollup.umd.js # generates npm version, also usable in require js app
@NODE_ENV=production $(ROLLUP) --config rollup.native.js # generates react native build
@NODE_ENV=production $(ROLLUP) --config rollup.umd.min.js
@NODE_ENV=production $(ROLLUP) --config rollup.min.js

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ A JavaScript SDK for tracking events and revenue to [Amplitude](https://www.ampl
* A [demo page](https://github.com/amplitude/GTM-Web-Demo) demonstrating a potential integration with Google Tag Manager.


## React Native
This library now supports react-native. It has two dependencies on react-native modules you will have to install yourself:
## Deprecated - React Native
As of >= v8.0.0, this SDK no longer has support for React Native. We recently released a new SDK focused on providing a React Native first approach to using Amplitude in cross-platform projects. [Amplitude React Native SDK](https://github.com/amplitude/Amplitude-ReactNative)

* [react-native-device-info](https://www.npmjs.com/package/react-native-device-info) Tested with version 3.1.4
* [@react-native-async-storage/async-storage](https://www.npmjs.com/package/@react-native-async-storage/async-storage) Tested with version 1.6.2
Previously used amplitude-js and looking to migrate to @amplitude/react-native?
* Please follow the steps detailed in our [migration guide](https://github.com/amplitude/Amplitude-ReactNative/blob/main/MIGRATION_GUIDE.md)

## Node.js
Please visit [Amplitude-Node](https://github.com/amplitude/Amplitude-Node) for our Node SDK.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"url": "https://github.com/amplitude/amplitude-javascript.git"
},
"main": "amplitude.umd.js",
"react-native": "amplitude.native.js",
"dependencies": {
"@amplitude/ua-parser-js": "0.7.24",
"@amplitude/utils": "^1.0.5",
Expand Down
1 change: 0 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default {
BUILD_COMPAT_SNIPPET: 'true',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
BUILD_COMPAT_REACT_NATIVE: 'false',
}),
commonjs(),
],
Expand Down
1 change: 0 additions & 1 deletion rollup.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default {
BUILD_COMPAT_SNIPPET: 'false',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
BUILD_COMPAT_REACT_NATIVE: 'false',
}),
commonjs({
include: "node_modules/**"
Expand Down
31 changes: 0 additions & 31 deletions rollup.native.js

This file was deleted.

1 change: 0 additions & 1 deletion rollup.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default {
BUILD_COMPAT_SNIPPET: 'true',
BUILD_COMPAT_LOCAL_STORAGE: 'true',
BUILD_COMPAT_2_0: 'true',
BUILD_COMPAT_REACT_NATIVE: 'false',
}),
commonjs(),
],
Expand Down
184 changes: 21 additions & 163 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ import DEFAULT_OPTIONS from './options';
import getHost from './get-host';
import baseCookie from './base-cookie';

let AsyncStorage;
let Platform;
let DeviceInfo;
if (BUILD_COMPAT_REACT_NATIVE) {
const reactNative = require('react-native');
AsyncStorage = require('@react-native-async-storage/async-storage').default;
Platform = reactNative.Platform;
DeviceInfo = require('react-native-device-info');
}

/**
* AmplitudeClient SDK API - instance constructor.
* The Amplitude class handles creation of client instances, all you need to do is call amplitude.getInstance()
Expand Down Expand Up @@ -203,75 +193,18 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this._isInitialized = true;
};

if (AsyncStorage) {
this._migrateUnsentEvents(() => {
Promise.all([
AsyncStorage.getItem(this._storageSuffix),
AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix),
AsyncStorage.getItem(this.options.unsentIdentifyKey + this._storageSuffix),
])
.then((values) => {
if (values[0]) {
const cookieData = JSON.parse(values[0]);
if (cookieData) {
_loadCookieDataProps(this, cookieData);
}
}
if (this.options.saveEvents) {
this._unsentEvents = this._parseSavedUnsentEventsString(values[1])
.map((event) => ({ event }))
.concat(this._unsentEvents);
this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2])
.map((event) => ({ event }))
.concat(this._unsentIdentifys);
}
if (DeviceInfo) {
Promise.all([
DeviceInfo.getCarrier(),
DeviceInfo.getModel(),
DeviceInfo.getManufacturer(),
DeviceInfo.getVersion(),
DeviceInfo.getUniqueId(),
])
.then((values) => {
this.deviceInfo = {
carrier: values[0],
model: values[1],
manufacturer: values[2],
version: values[3],
};
initFromStorage(values[4]);
this.runQueuedFunctions();
if (type(opt_callback) === 'function') {
opt_callback(this);
}
})
.catch((err) => {
this.options.onError(err);
});
} else {
initFromStorage();
this.runQueuedFunctions();
}
})
.catch((err) => {
this.options.onError(err);
});
});
} else {
if (this.options.saveEvents) {
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey)
.map((event) => ({ event }))
.concat(this._unsentEvents);
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey)
.map((event) => ({ event }))
.concat(this._unsentIdentifys);
}
initFromStorage();
this.runQueuedFunctions();
if (type(opt_callback) === 'function') {
opt_callback(this);
}
if (this.options.saveEvents) {
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey)
.map((event) => ({ event }))
.concat(this._unsentEvents);
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey)
.map((event) => ({ event }))
.concat(this._unsentIdentifys);
}
initFromStorage();
this.runQueuedFunctions();
if (type(opt_callback) === 'function') {
opt_callback(this);
}
} catch (err) {
utils.log.error(err);
Expand Down Expand Up @@ -339,53 +272,6 @@ const _validateUnsentEventQueue = (queue) => {
}
};

/**
* @private
*/
AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(cb) {
Promise.all([AsyncStorage.getItem(this.options.unsentKey), AsyncStorage.getItem(this.options.unsentIdentifyKey)])
.then((values) => {
if (this.options.saveEvents) {
var unsentEventsString = values[0];
var unsentIdentifyKey = values[1];

var itemsToSet = [];
var itemsToRemove = [];

if (unsentEventsString) {
itemsToSet.push(
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(unsentEventsString)),
);
itemsToRemove.push(AsyncStorage.removeItem(this.options.unsentKey));
}

if (unsentIdentifyKey) {
itemsToSet.push(
AsyncStorage.setItem(
this.options.unsentIdentifyKey + this._storageSuffix,
JSON.stringify(unsentIdentifyKey),
),
);
itemsToRemove.push(AsyncStorage.removeItem(this.options.unsentIdentifyKey));
}

if (itemsToSet.length > 0) {
Promise.all(itemsToSet)
.then(() => {
Promise.all(itemsToRemove);
})
.catch((err) => {
this.options.onError(err);
});
}
}
})
.then(cb)
.catch((err) => {
this.options.onError(err);
});
};

/**
* @private
*/
Expand Down Expand Up @@ -718,9 +604,6 @@ var _saveCookieData = function _saveCookieData(scope) {
identifyId: scope._identifyId,
sequenceNumber: scope._sequenceNumber,
};
if (AsyncStorage) {
AsyncStorage.setItem(scope._storageSuffix, JSON.stringify(cookieData));
}

if (scope._useOldCookie) {
scope.cookieStorage.set(scope.options.cookieName + scope._storageSuffix, cookieData);
Expand Down Expand Up @@ -869,21 +752,13 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() {
try {
const serializedUnsentEvents = JSON.stringify(this._unsentEvents.map(({ event }) => event));

if (AsyncStorage) {
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, serializedUnsentEvents);
} else {
this._setInStorage(localStorage, this.options.unsentKey, serializedUnsentEvents);
}
this._setInStorage(localStorage, this.options.unsentKey, serializedUnsentEvents);
} catch (e) {} /* eslint-disable-line no-empty */

try {
const serializedIdentifys = JSON.stringify(this._unsentIdentifys.map((unsentIdentify) => unsentIdentify.event));

if (AsyncStorage) {
AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, serializedIdentifys);
} else {
this._setInStorage(localStorage, this.options.unsentIdentifyKey, serializedIdentifys);
}
this._setInStorage(localStorage, this.options.unsentIdentifyKey, serializedIdentifys);
} catch (e) {} /* eslint-disable-line no-empty */
};

Expand Down Expand Up @@ -1257,9 +1132,8 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
timestamp,
callback,
) {
if (!BUILD_COMPAT_REACT_NATIVE) {
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
}
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs

if (!eventType) {
if (type(callback) === 'function') {
callback(0, 'No request sent', { reason: 'Missing eventType' });
Expand Down Expand Up @@ -1288,23 +1162,9 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
this._lastEventTime = eventTime;
_saveCookieData(this);

let osName = this._ua.browser.name;
let osVersion = this._ua.browser.major;
let deviceModel = this._ua.os.name;
let deviceManufacturer;

let versionName;
let carrier;
if (BUILD_COMPAT_REACT_NATIVE) {
osName = Platform.OS;
osVersion = Platform.Version;
if (this.deviceInfo) {
carrier = this.deviceInfo.carrier;
deviceManufacturer = this.deviceInfo.manufacturer;
deviceModel = this.deviceInfo.model;
versionName = this.deviceInfo.version;
}
}
const osName = this._ua.browser.name;
const osVersion = this._ua.browser.major;
const deviceModel = this._ua.os.name;

userProperties = userProperties || {};
var trackingOptions = { ...this._apiPropertiesTrackingOptions };
Expand All @@ -1319,20 +1179,18 @@ AmplitudeClient.prototype._logEvent = function _logEvent(
event_id: eventId,
session_id: this._sessionId || -1,
event_type: eventType,
version_name: _shouldTrackField(this, 'version_name') ? this.options.versionName || versionName || null : null,
version_name: this.options.versionName || null,
platform: _shouldTrackField(this, 'platform') ? this.options.platform : null,
os_name: _shouldTrackField(this, 'os_name') ? osName || null : null,
os_version: _shouldTrackField(this, 'os_version') ? osVersion || null : null,
device_model: _shouldTrackField(this, 'device_model') ? deviceModel || null : null,
device_manufacturer: _shouldTrackField(this, 'device_manufacturer') ? deviceManufacturer || null : null,
language: _shouldTrackField(this, 'language') ? this.options.language : null,
carrier: _shouldTrackField(this, 'carrier') ? carrier || null : null,
api_properties: apiProperties,
event_properties: utils.truncate(utils.validateProperties(eventProperties)),
user_properties: utils.truncate(utils.validateProperties(userProperties)),
uuid: UUID(),
library: {
name: BUILD_COMPAT_REACT_NATIVE ? 'amplitude-react-native' : 'amplitude-js',
name: 'amplitude-js',
version: version,
},
sequence_number: sequenceNumber, // for ordering events and identifys
Expand Down
6 changes: 2 additions & 4 deletions src/metadata-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ class MetadataStorage {

this.cookieDomain = '';

if (!BUILD_COMPAT_REACT_NATIVE) {
const writableTopDomain = topDomain(getLocation().href);
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);
}
const writableTopDomain = topDomain(getLocation().href);
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);

if (storageOptionExists[storage]) {
this.storage = storage;
Expand Down
Loading