Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: React detection and Analytics batch upload #11478

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
3 changes: 1 addition & 2 deletions packages/analytics/src/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const trackers = {
};

type TrackerTypes = keyof typeof trackers;
type Trackers = typeof trackers[TrackerTypes];
type Trackers = (typeof trackers)[TrackerTypes];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had made a change to this file I rolled back, but prettier is really insistent that this line be changed... 🙄

let _instance = null;

/**
Expand Down Expand Up @@ -273,7 +273,6 @@ export class AnalyticsClass {
}

const provider = params.provider ? params.provider : 'AWSPinpoint';

return new Promise((resolve, reject) => {
this._pluggables.forEach(pluggable => {
if (pluggable.getProviderName() === provider) {
Expand Down
9 changes: 7 additions & 2 deletions packages/analytics/src/Providers/EventBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ConsoleLogger as Logger } from '@aws-amplify/core';
import { AnalyticsAction, ConsoleLogger as Logger } from '@aws-amplify/core';
import {
putEvents,
PutEventsInput,
PutEventsOutput,
} from '@aws-amplify/core/internals/aws-clients/pinpoint';
import { EventBuffer, EventObject, EventMap } from '../types';
import { isAppInForeground } from '../utils/AppUtils';
import { getAnalyticsUserAgentString } from '../utils/UserAgent';

const logger = new Logger('EventsBuffer');
const RETRYABLE_CODES = [429, 500];
Expand Down Expand Up @@ -101,7 +102,11 @@ export default class EventsBuffer {
try {
const { credentials, region } = this._config;
const data: PutEventsOutput = await putEvents(
{ credentials, region },
{
credentials,
region,
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to track how often Record is called on Analytics or how often events are batch sent to Pinpoint?

},
batchEventParams
);
this._processPutEventsSuccessResponse(data, eventMap);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"name": "Core (Credentials)",
"path": "./lib-esm/index.js",
"import": "{ Credentials }",
"limit": "13.15 kB"
"limit": "13.2 kB"
},
{
"name": "Core (Signer)",
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/Platform/detection/React.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { documentExists, processExists, windowExists } from './helpers';
// Tested with react 18.2 - built using Vite

export function reactWebDetect() {
return documentExists() && !!document.getElementById('react-root');
const elementKeyPrefixedWithReact = k => {
return k.startsWith('_react') || k.startsWith('__react');
};
const elementIsReactEnabled = e => {
return Object.keys(e).find(elementKeyPrefixedWithReact);
};
const allElementsWithId = () => Array.from(document.querySelectorAll('[id]'));
Comment on lines +9 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit since this doesn't run often to my understanding but typically we'd want to extract these so they're not recreated every time reactWebDetect() is run.

Second nit would be to use more meaningful parameter names... it's easy enough to intuit that k is key or e is element but it's even easier when it's spelled out for you.


return documentExists() && allElementsWithId().some(elementIsReactEnabled);
}

export function reactSSRDetect() {
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"name": "Geo (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Geo }",
"limit": "51.5 kB"
"limit": "51.6 kB"
}
],
"jest": {
Expand Down