# sensorsdata/sa-sdk-javascript architecture

> Architecture diagram of the GitHub repository sensorsdata/sa-sdk-javascript, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sensorsdata/sa-sdk-javascript
- Repository: https://github.com/sensorsdata/sa-sdk-javascript

## Overview

##### Main Components
- Core SDK: Identify the “core” of the project (e.g., the “core” directory and core logic encapsulated in the main JavaScript files present in the distribution files such as sensorsdata.js, sensorsdata.amd.js, and sensorsdata.es6.js). This is the base layer responsible for data collection, processing, and exposing the public API.
- Distribution Bundles: Note the “dist” folder with its subdirectories “v2” and “web”. The “web” build (with detailed JavaScript files and type definitions) is tailored for browser-based applications. The “v2” version might represent an updated or alternative build. Include them as different build outputs or deployment targets.
- Plugin Framework: Under dist/web/plugin, there is a rich directory structure of plugins (such as ajax-sender, aes-encryption, android-bridge, ios-bridge, pageleave, etc.). Each plugin represents a modular enhancement to the core analytics functionality (e.g., sending data via beacon, supporting various encryption schemes, or bridging data collection between environments).

##### Relationships and Interactions
- Data is captured by the Core.
- Plugins (like ajax-sender or beacon-sender) “hook” into the process to deliver the events externally.

##### Architectural Patterns and Design Principles
- Modularity: The design is highly modular. The Core SDK is kept independent of the plugin implementations, facilitating easy extension or replacement.
- Separation of Concerns: The core logic is separate from the extended behaviors (like various sending mechanisms, encryption, platform-specific bridges). This separation should be clearly reflected, grouping plugins into one area and the core logic into another.
- Multi-target Distribution: There is a clear pattern of supporting various module systems (AMD, ES6) and the concept of multiple builds (v2 versus web) is present, so it may be useful to show these as different “ports” or “interfaces” of the SDK.

##### Relevant Technologies and Libraries
- JavaScript and ES6: The presence of multiple *.es6.js files indicates modern JavaScript development.
- Module Systems: Note that the SDK supports AMD and potentially other module loading systems.
- Plugin-based architecture: Many plugins with their own build output and documentation highlight the extensible design.

##### Essential Elements
- Grouping:
- “web” plugins together under a “Plugins” section.
- Plugins by functionality (e.g., encryption, event sending, bridging).
- Clear Labels: Ensure that labels detail both functionality and possibly names as seen in the file tree (e.g., “ajax-sender”, “ios-bridge”).

To summarize for the principal software engineer:

This approach should help capture all the essential architectural components and their interactions, tailored specifically for the SensorsData JavaScript SDK project.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core & Distribution Layer
    subgraph "Core & Distribution"
        CS["Core SDK"]:::core
        DV2["Distribution Bundle (v2)"]:::dist
        DW["Distribution Bundle (web)"]:::dist
    end

    %% Plugin Framework Layer
    subgraph "Plugin Framework"
        Ajax["ajax-sender"]:::plugin
        AES["aes-encryption"]:::plugin
        Android["android-bridge"]:::plugin
        iOS["ios-bridge"]:::plugin
        PageLeave["pageleave"]:::plugin
    end

    %% Configuration & Versioning Layer
    subgraph "Configuration & Versioning"
        PKG["package.json"]:::config
        VER["version.js"]:::config
        CHLOG["CHANGELOG.md"]:::config
        LIC["LICENSE"]:::config
        GIT[".gitignore"]:::config
    end

    %% Connections between Core and Distribution
    CS -->|"distributes_to"| DV2
    CS -->|"distributes_to"| DW

    %% Core extends functionality using Plugins
    CS -->|"extends"| Ajax
    CS -->|"extends"| AES
    CS -->|"extends"| Android
    CS -->|"extends"| iOS
    CS -->|"extends"| PageLeave

    %% Configuration supports Core functionality
    PKG -->|"configures"| CS
    VER -->|"configures"| CS
    CHLOG -->|"documents"| CS
    LIC -->|"licenses"| CS
    GIT -->|"ignores"| CS

    %% Click Events for Core & Distribution
    click CS "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/core"
    click DV2 "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/v2"
    click DW "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web"

    %% Click Events for Plugin Framework
    click Ajax "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web/plugin/ajax-sender"
    click AES "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web/plugin/aes-encryption"
    click Android "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web/plugin/android-bridge"
    click iOS "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web/plugin/ios-bridge"
    click PageLeave "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/dist/web/plugin/pageleave"

    %% Click Events for Configuration & Versioning
    click PKG "https://github.com/sensorsdata/sa-sdk-javascript/blob/master/package.json"
    click VER "https://github.com/sensorsdata/sa-sdk-javascript/blob/master/version.js"
    click CHLOG "https://github.com/sensorsdata/sa-sdk-javascript/blob/master/CHANGELOG.md"
    click LIC "https://github.com/sensorsdata/sa-sdk-javascript/tree/master/LICENSE"
    click GIT "https://github.com/sensorsdata/sa-sdk-javascript/blob/master/.gitignore"

    %% Styles
    classDef core fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef dist fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef plugin fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef config fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
