Skip to content

feat: add network tracking support #270

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

Merged
merged 4 commits into from
Apr 24, 2025
Merged

Conversation

sojingle
Copy link
Collaborator

@sojingle sojingle commented Apr 11, 2025

Summary

Add a new autocapture option -- network tracking

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: no

@sojingle sojingle force-pushed the AMP-125615-network-tracking branch 11 times, most recently from 33da1f8 to 65c883f Compare April 16, 2025 21:33
@sojingle sojingle force-pushed the AMP-125615-network-tracking branch 2 times, most recently from 7a24460 to 5d45930 Compare April 22, 2025 21:24
@sojingle sojingle marked this pull request as ready for review April 22, 2025 21:24
@sojingle sojingle requested review from crleona and Copilot April 22, 2025 21:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces network tracking support into the SDK by adding new network tracking options and integrating them via method swizzling and a dedicated plugin. Key changes include:

  • Adding new files for network tracking (NetworkSwizzler.swift, NetworkTrackingPlugin.swift) and updating configuration & constants.
  • Updating autocapture options, HTTP client setup, and example SwiftUI views to incorporate network tracking.
  • Enhancing tests with FakeURLProtocol and AutocaptureOptionsTests to verify the new network tracking behavior.

Reviewed Changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Tests/AmplitudeTests/Utilities/MethodSwizzlerTest.swift Added tests for method swizzling functionality.
Tests/AmplitudeTests/Supports/FakeURLProtocol.swift New fake protocol for simulating network responses.
Tests/AmplitudeTests/AutocaptureOptionsTests.swift Updated tests to check that network tracking is disabled on watchOS.
Sources/Amplitude/Utilities/NetworkSwizzler.swift New swizzler to intercept URLSessionTask resume and state changes.
Sources/Amplitude/Plugins/NetworkTrackingPlugin.swift New plugin for network tracking that hooks into task resume/state events.
Sources/Amplitude/Constants.swift Added constants to support network tracking events and properties.
Sources/Amplitude/Configuration.swift Expanded configuration to include networkTrackingOptions and protocol class injection.
Sources/Amplitude/AutocaptureOptions.swift Extended autocapture options with a new network tracking flag.
Sources/Amplitude/Amplitude.swift Integrated NetworkTrackingPlugin and swizzling into the main SDK flow.
Examples/AmplitudeSwiftUIExample/ContentView.swift & AmplitudeSwiftUIExampleApp.swift UI updates to allow testing of network tracking via user inputs.
Files not reviewed (1)
  • Amplitude-Swift.xcodeproj/project.pbxproj: Language not supported

@sojingle sojingle force-pushed the AMP-125615-network-tracking branch 2 times, most recently from 93feede to 53419dc Compare April 22, 2025 21:34
@sojingle sojingle requested a review from Copilot April 22, 2025 21:37
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new network tracking feature for the Amplitude SDK by adding a NetworkTrackingPlugin and supporting utilities such as NetworkSwizzler, new configuration options, and additional tests. Key changes include:

  • Implementation of network tracking support via method swizzling on URLSessionTask.
  • Addition of network tracking options in configuration and autocapture, with related constants and events.
  • New and updated tests covering method swizzling, fake URL responses, and network tracking UI elements.

Reviewed Changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/AmplitudeTests/Utilities/MethodSwizzlerTest.swift Added tests for method swizzling behavior with new network tracking methods.
Tests/AmplitudeTests/Supports/FakeURLProtocol.swift Introduced a fake URL protocol for simulating network responses.
Tests/AmplitudeTests/AutocaptureOptionsTests.swift Updated tests to conditionally verify network tracking in autocapture options.
Sources/Amplitude/Utilities/NetworkSwizzler.swift Added swizzling for URLSessionTask’s resume and setState to hook network operations.
Sources/Amplitude/Plugins/NetworkTrackingPlugin.swift Added the network tracking plugin that listens to URLSessionTask state changes and tracks network events.
Sources/Amplitude/Configuration.swift Updated configuration to include networkTrackingOptions.
Sources/Amplitude/AutocaptureOptions.swift Extended autocapture options to support network tracking.
Sources/Amplitude/Amplitude.swift Integrated the NetworkTrackingPlugin and swizzling into the main Amplitude flow.
Examples/... Added UI elements for testing network tracking from AmplitudeSwiftUIExample.
Files not reviewed (1)
  • Amplitude-Swift.xcodeproj/project.pbxproj: Language not supported
Comments suppressed due to low confidence (3)

Tests/AmplitudeTests/Utilities/MethodSwizzlerTest.swift:28

  • [nitpick] Consider adding a comment here to clarify that this recursive call resolves to the original method implementation due to swizzling, which may not be immediately obvious to future maintainers.
_ = swizzledMethodA()

Tests/AmplitudeTests/AutocaptureOptionsTests.swift:13

  • [nitpick] Consider adding additional tests to validate network tracking event capture behavior, particularly for cases where host matching either succeeds or fails against the capture rules.
XCTAssertFalse(config.autocapture.contains(.networkTracking))

Sources/Amplitude/Utilities/NetworkSwizzler.swift:65

  • [nitpick] Consider adding an inline comment explaining that this call triggers the original 'resume' implementation via method swizzling, which helps clarify the indirect recursion.
amp_resume()

@sojingle sojingle requested a review from crleona April 23, 2025 05:12
@sojingle sojingle force-pushed the AMP-125615-network-tracking branch from b736146 to 256a32c Compare April 23, 2025 21:18
@sojingle sojingle requested a review from crleona April 23, 2025 23:33
@sojingle sojingle merged commit d1647b3 into main Apr 24, 2025
9 checks passed
@sojingle sojingle deleted the AMP-125615-network-tracking branch April 24, 2025 17:14
github-actions bot pushed a commit that referenced this pull request Apr 25, 2025
# [1.12.0](v1.11.10...v1.12.0) (2025-04-25)

### Bug Fixes

* apply identifies to identity userProperties in correct order ([#272](#272)) ([41deb6a](41deb6a))
* clean up ios observers on stop ([#275](#275)) ([3965e13](3965e13))
* disallow setting nulls for various properties dicts, as this is not supported ([#277](#277)) ([70ea4be](70ea4be))
* enable running tests from package.swift ([#274](#274)) ([f7aad5b](f7aad5b))
* move default init properties to new defaults structure so they can be shared ([#276](#276)) ([88e3b16](88e3b16))
* remove unused enableRemoteConfig option ([#278](#278)) ([c9a2608](c9a2608))
* treat non-op user properties as sets ([#273](#273)) ([df65e7b](df65e7b))

### Features

* add network tracking support ([#270](#270)) ([d1647b3](d1647b3))
* add ObjC interface for NetworkTrackingOptions ([#281](#281)) ([8f699fa](8f699fa))
* unify several identity apis into a single Identity struct ([#268](#268)) ([ceae1e4](ceae1e4))
Copy link

🎉 This PR is included in version 1.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants