Amplitude Analytics确实提供了Javascript SDK for Amplitude,但是如何在无法直接访问DOM的React App中实现分析?
GitHub页面建议使用amplitude.getInstance().logEvent('EVENT_IDENTIFIER_HERE')
但是在React中,我没有组件或事件的唯一标识符。
最佳答案
这就是我所做的,并且效果很好:yarn add amplitude-js
实用程序/amplitude.js
import amplitude from 'amplitude-js/amplitude';
export const initAmplitude = () => {
amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE);
};
export const setAmplitudeUserDevice = installationToken => {
amplitude.getInstance().setDeviceId(installationToken);
};
export const setAmplitudeUserId = userId => {
amplitude.getInstance().setUserId(userId);
};
export const setAmplitudeUserProperties = properties => {
amplitude.getInstance().setUserProperties(properties);
};
export const sendAmplitudeData = (eventType, eventProperties) => {
amplitude.getInstance().logEvent(eventType, eventProperties);
};
index.js
...
import { initAmplitude } from './utilities/amplitude';
initAmplitude();
ReactDOM.render(
<ThemeProvider theme={theme}>
<Provider store={store}>
<Routes store={store} />
</Provider>
</ThemeProvider>,
document.getElementById('root')
);
然后您就可以开始了。在需要时调用其他方法,例如
setAmplitudeUserDevice
:import { setAmplitudeUserDevice } from 'utilities/amplitude';
export function installationInitializationSuccess(id, token) {
setAmplitudeUserDevice(token);
return {
type: INSTALLATION_INITIALIZATION_SUCCESS,
id,
token
};
}
希望能帮助到你!