aws / aws-sdk-mobile-analytics-js

Amazon Mobile Analytics JavaScript SDK
Apache License 2.0
82 stars 38 forks source link

Packaged size too large #23

Closed tonygentilcore closed 6 years ago

tonygentilcore commented 7 years ago

I believe that this is the minimal set of JS necessary to use mobile analytics:

$ ls -l
total 488
-rw-r--r--@ 1 tony  staff  222015 Aug 23 18:03 aws-sdk-2.5.3.min.js
-rw-r--r--@ 1 tony  staff   23450 Aug 23 17:57 aws-sdk-mobile-analytics.min.js

After webpack, it adds 246K to the bundle size. That's an order of magnitude larger than I'd expect to budget for analytics. To build that SDK, I downloaded the minimized bundle w/ only AWS.CognitoIdentity. With the latest, 2.6.7, it's slightly larger (~227k).

Is there anything I could be doing better here? Or is there anything this repo could do to avoid this bloated dependency that's clearly not meant for deployment in a mobile app?

richabra commented 7 years ago

Currently the Mobile Analytics SDK is integrated in the Mobile SDK. We are continuously working on improvements, but we cannot comment at this time about addressing the size issue of our SDK. If the size is currently too large for you, our recommendation is to check out our Rest API and to call that directly. http://docs.aws.amazon.com/mobileanalytics/latest/ug/server-reference.html. Sorry for the inconvenience.

tonygentilcore commented 7 years ago

In the interim, it'd be useful to document (or better yet, link to) the minimal SDK build. I think that looks like this:

screen shot 2016-12-01 at 10 15 15 am
SamPlacette commented 7 years ago

I looked into this several months ago and I seem to remember that one of the big sources, if not the primary source, of the large package size was the Node.js crypto lib.

A browser-optimized build under 20KB as @tonygentilcore mentioned would be very helpful for deploying this to web environments.

Does anyone know of any forks to this project that has replaced the Node.js crypto lib dependency with the browser Crypto lib? Or any other browser-optimized projects that consume the AWS Mobile Analytics API?

Thanks Sam

SamPlacette commented 7 years ago

@richabra I am working on a project which brings the total compiled size of the AWS SDK MobileAnalytics JS library down to less than 60kb minified. I am doing this by inserting an optimized subset of the core AWS SDK in place of the actual core SDK library at build time. It essentially replaces the core AWS SDK with a stripped down "stub" version that directly calls the REST API (and it only supports Cognito and MobileAnalytics requests).

This approach "works", but it requires some special build-time modifications in order to substitute the AWS stub for the actual AWS lib. It would be much easier for a diverse array of audiences to include in their project if the MobileAnalytics team supported the ability to inject a reference to the AWS library dependency as a configuration parameter. The initial download size could also be significantly reduced if the MobileAnalytics JS library included an asynchronous bootstrap option, like the Google Analytics "isogram" snippet, which loads minimal dependencies in order to collect and queue events locally while the rest of the dependencies load asynchronously. Any chance you could accommodate these changes? I think AWS MobileAnalytics is a strong product for Mobile, and with these changes it could be viable for Web (and Mobile Web, Hybrid, etc) applications too.

SamPlacette commented 7 years ago

@richabra @tonygentilcore I published an initial draft of the browser-optimized AWS Mobile Analytics client to https://github.com/karmies/aws-mobile-analytics-lite and https://www.npmjs.com/package/aws-mobile-analytics-lite . This actually uses the full AWS Mobile Analytics JS client project, but swaps out the AWS core SDK for the bare minimum necessary to record events. The full build is under 50KB minified, and I also have an async bootstrap entry point which is under 2KB.

@richabra now that I have implemented this I think I probably initially overstated how helpful the dependency injection feature would be. It would lower the barrier of entry for a certain class of users, but it's not like a requirement for people to use the project.

I also ended up implementing the async bootstrap feature that I requested above; it is OK but not great (for example the timestamp is not correct and there are probably some other more subtle flaws with this approach I have not yet noticed). I think if this approach was supported within the aws-sdk-mobile-analytics-js project it could be much more robust and less fragile when the software is updated down the road. Do you have any interest in tweaking the aws-sdk-mobile-analytics-js project to better support this async loading use case?

WChoy commented 6 years ago

The "package.json" should have a "dependencies": { "aws-sdk": "^2.2.37" }, not "dependencies": { "aws-sdk": ">=2.2.37" }, the later will download the LASTEST aws-sdk into the "./node_modules/aws-sdk-mobile-analytics/node_modules/aws-sdk/dist/aws-sdk.js".

Problem is the AWS-SDK does not match the version in my desired "./node_modules/aws-sdk". In my case, the most recent is causing a "STREAM" not found in my React-Native application (crypto lib is using a library found in node.js core).

cheruvian commented 6 years ago

Amazon Mobile Analytics is not Amazon Pinpoint!

The official recommended JS sdk for Amazon Pinpoint Analytics is AWS Amplify which has a significantly reduced footprint. (It still records to Amazon Mobile Analytics as well!)