Skip to content

Commit

Permalink
feat: add path property to custom event (#329)
Browse files Browse the repository at this point in the history
* feat: add path property to custom event

* style: code format

* fix: last page path on Flutter

* fix: no empty path for custom event

* test: update demo

* fix: custom event path with tracker only

* feat: add config customEventWithPage

* fix: rename customEventWithPage to referCustomEventWithPage

* fix: rename referCustomEventWithPage to customEventWithPath

* fix: customEventWithPath

* fix: hybrid custom event path

---------

Co-authored-by: GIOSDK <[email protected]>
  • Loading branch information
YoloMao and GIOSDK authored Sep 29, 2024
1 parent 997c87c commit 05a01a8
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 153 deletions.
5 changes: 5 additions & 0 deletions Example/Example-watchOS Watch App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import Foundation
import WatchKit
#if canImport(GrowingAnalytics)
import GrowingAnalytics
#endif
#if canImport(GrowingTracker)
import GrowingTracker
#endif

class AppDelegate: NSObject, WKApplicationDelegate {
func applicationDidFinishLaunching() {
Expand Down
5 changes: 5 additions & 0 deletions Example/Example-watchOS Watch App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// limitations under the License.

import SwiftUI
#if canImport(GrowingAnalytics)
import GrowingAnalytics
#endif
#if canImport(GrowingTracker)
import GrowingTracker
#endif

struct ContentView: View {
var body: some View {
Expand Down
5 changes: 5 additions & 0 deletions Example/Example-watchOS Watch App/Example_watchOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// limitations under the License.

import SwiftUI
#if canImport(GrowingAnalytics)
import GrowingAnalytics
#endif
#if canImport(GrowingTracker)
import GrowingTracker
#endif

@main
struct Example_watchOS_Watch_AppApp: App {
Expand Down
6 changes: 6 additions & 0 deletions Example/Example/GrowingIO-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
#define SDKABTESTINGMODULE
#endif

#if __has_include("UIView+GrowingImpression.h")
// ImpressionTrack Module
#import "UIView+GrowingImpression.h"
#define SDKIMPMODULE
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#import "AppDelegate.h"
#if defined(AUTOTRACKER)
#if defined(SDK3rd)
#if defined(SDKIMPMODULE)
#import "UIView+GrowingImpression.h"
#endif
#endif
#endif

@interface GIOClickEventViewController ()

Expand All @@ -37,8 +39,10 @@ - (void)viewDidLoad {
#if defined(SDK3rd)
[[GrowingAutotracker sharedInstance] autotrackPage:self alias:@"点击事件测试" attributes:@{@"greet": @"hello"}];
self.sendEventButton.growingUniqueTag = @"UniqueTag-SendButton";
#if defined(SDKIMPMODULE)
[self.sendEventButton growingTrackImpression:@"hello_track_impression"];
[self.view growingTrackImpression:@"self_view_imp_track" attributes:@{@"self_view_key": @"self_view_value"}];
#endif
self.CView.growingUniqueTag = @"CCCCC";
self.DView.growingUniqueTag = @"DDDDD";
self.EView.growingUniqueTag = @"EEEEE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ - (void)testGrowingHybridPageEvent {
XCTAssertEqualObjects(event.attributes[@"test"], @"value");
}

- (void)testGrowingPageCustomEvent {
GrowingPageCustomEvent *event = (GrowingPageCustomEvent *)GrowingPageCustomEvent.builder.setPath(@"path")
.setEventName(@"testEventName")
.setAttributes(@{@"test": @"value"})
.build;

XCTAssertEqualObjects(event.path, @"path");
XCTAssertEqualObjects(event.eventName, @"testEventName");
NSString *value = (NSString *)event.attributes[@"test"];
XCTAssertTrue([value isEqualToString:@"value"]);
}

- (void)testGrowingHybridViewElementEvent {
GrowingHybridViewElementEvent *event =
(GrowingHybridViewElementEvent *)GrowingHybridViewElementEvent.builder.setHyperlink(@"testHyperlink")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// limitations under the License.

#import <XCTest/XCTest.h>
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageCustomEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingViewElementEvent.h"
#import "GrowingTrackerCore/Event/GrowingAppCloseEvent.h"
Expand Down Expand Up @@ -97,6 +96,7 @@ - (void)testEventConvertToPB_Custom {
// GrowingCustomEvent
{
GrowingBaseBuilder *builder = GrowingCustomEvent.builder.setEventName(@"custom")
.setPath(@"path")
.setAttributes(@{@"key": @"value"});
[builder readPropertyInTrackThread];
GrowingCustomEvent *event = (GrowingCustomEvent *)(builder.build);
Expand All @@ -105,6 +105,7 @@ - (void)testEventConvertToPB_Custom {
XCTAssertEqualObjects(GrowingEventTypeCustom, event.eventType);
XCTAssertEqual(GrowingPBEventType_Custom, protobuf.eventType);
XCTAssertEqualObjects(event.eventName ?: @"", protobuf.eventName);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
{
Expand All @@ -116,33 +117,6 @@ - (void)testEventConvertToPB_Custom {
XCTAssertEqualObjects(GrowingEventTypeCustom, event.eventType);
XCTAssertEqual(GrowingPBEventType_Custom, protobuf.eventType);
XCTAssertEqualObjects(event.eventName ?: @"", protobuf.eventName);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}

// GrowingPageCustomEvent
{
GrowingBaseBuilder *builder = GrowingPageCustomEvent.builder.setEventName(@"custom")
.setPath(@"path")
.setAttributes(@{@"key": @"value"});
[builder readPropertyInTrackThread];
GrowingPageCustomEvent *event = (GrowingPageCustomEvent *)(builder.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypeCustom, event.eventType);
XCTAssertEqual(GrowingPBEventType_Custom, protobuf.eventType);
XCTAssertEqualObjects(event.eventName ?: @"", protobuf.eventName);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
{
GrowingBaseBuilder *builder = GrowingPageCustomEvent.builder;
[builder readPropertyInTrackThread];
GrowingPageCustomEvent *event = (GrowingPageCustomEvent *)(builder.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypeCustom, event.eventType);
XCTAssertEqual(GrowingPBEventType_Custom, protobuf.eventType);
XCTAssertEqualObjects(event.eventName ?: @"", protobuf.eventName);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
Expand Down
30 changes: 29 additions & 1 deletion GrowingAutotrackerCore/Page/GrowingPageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "GrowingAutotrackerCore/Autotrack/UIViewController+GrowingAutotracker.h"
#import "GrowingAutotrackerCore/GrowingNode/Category/UIViewController+GrowingNode.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "GrowingTrackerCore/Event/GrowingCustomEvent.h"
#import "GrowingTrackerCore/Event/GrowingEventManager.h"
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
Expand All @@ -30,8 +31,10 @@
#import "GrowingULAppLifecycle.h"
#import "GrowingULViewControllerLifecycle.h"

@interface GrowingPageManager () <GrowingULViewControllerLifecycleDelegate>
@interface GrowingPageManager () <GrowingULViewControllerLifecycleDelegate, GrowingEventInterceptor>

@property (nonatomic, copy) NSString *lastPagePath;
@property (nonatomic, assign) long long lastPageTimestamp;
@property (nonatomic, strong) NSPointerArray *visiblePages;
@property (nonatomic, strong) NSMutableArray<NSString *> *ignoredPrivateControllers;

Expand All @@ -52,10 +55,35 @@ + (instancetype)sharedInstance {
- (void)start {
static dispatch_once_t startOnceToken;
dispatch_once(&startOnceToken, ^{
GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration;
if (configuration.customEventWithPath) {
[[GrowingEventManager sharedInstance] addInterceptor:self];
}
[GrowingULViewControllerLifecycle.sharedInstance addViewControllerLifecycleDelegate:self];
});
}

#pragma mark - GrowingEventInterceptor

- (void)growingEventManagerEventWillBuild:(GrowingBaseBuilder *_Nullable)builder {
if (builder) {
if ([builder isMemberOfClass:[GrowingCustomBuilder class]]) {
// Hybrid触发的HybridCustomEvent将在生成时携带path,不走此处的逻辑
// Flutter触发的CustomEvent与原生相同,在此处判断后携带path
if (self.lastPagePath && self.lastPagePath.length > 0) {
((GrowingCustomBuilder *)builder).setPath(self.lastPagePath.copy);
}
} else if ([builder isMemberOfClass:[GrowingPageBuilder class]]) {
// 原生或Flutter生成PAGE事件
GrowingPageBuilder *pageBuilder = (GrowingPageBuilder *)builder;
self.lastPagePath = pageBuilder.path;
self.lastPageTimestamp = pageBuilder.timestamp;
}
}
}

#pragma mark - GrowingULViewControllerLifecycleDelegate

- (void)viewControllerDidLoad:(UIViewController *)controller {
if (controller.growingPageAlias == nil /* !page.isAutotrack */) {
// 首次进入该controller,获取初始化autotrackPage配置
Expand Down
46 changes: 0 additions & 46 deletions GrowingTrackerCore/Event/Autotrack/GrowingPageCustomEvent.h

This file was deleted.

60 changes: 0 additions & 60 deletions GrowingTrackerCore/Event/Autotrack/GrowingPageCustomEvent.m

This file was deleted.

4 changes: 3 additions & 1 deletion GrowingTrackerCore/Event/GrowingCustomEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface GrowingCustomEvent : GrowingBaseEvent

@property (nonatomic, copy) NSString *eventName;
@property (nonatomic, copy) NSString *path;

+ (GrowingCustomBuilder *)builder;

Expand All @@ -35,9 +36,10 @@ NS_ASSUME_NONNULL_BEGIN
@interface GrowingCustomBuilder : GrowingBaseBuilder

@property (nonatomic, copy, readonly) NSString *eventName;
@property (nonatomic, copy, readonly) NSString *path;

- (GrowingCustomBuilder * (^)(NSString *value))setEventName;

- (GrowingCustomBuilder * (^)(NSString *value))setPath;
- (GrowingCustomBuilder * (^)(NSDictionary<NSString *, NSObject *> *value))setAttributes;

@end
Expand Down
18 changes: 18 additions & 0 deletions GrowingTrackerCore/Event/GrowingCustomEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

#import "GrowingTrackerCore/Event/GrowingCustomEvent.h"
#import "GrowingTrackerCore/Event/GrowingTrackEventType.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"

@implementation GrowingCustomEvent

- (instancetype)initWithBuilder:(GrowingBaseBuilder *)builder {
if (self = [super initWithBuilder:builder]) {
GrowingCustomBuilder *subBuilder = (GrowingCustomBuilder *)builder;
_eventName = subBuilder.eventName;
_path = subBuilder.path;
}
return self;
}
Expand All @@ -37,6 +39,7 @@ + (GrowingCustomBuilder *)builder {
- (NSDictionary *)toDictionary {
NSMutableDictionary *dataDictM = [NSMutableDictionary dictionaryWithDictionary:[super toDictionary]];
dataDictM[@"eventName"] = self.eventName;
dataDictM[@"path"] = self.path;
return [dataDictM copy];
}

Expand All @@ -46,6 +49,14 @@ - (NSDictionary *)toDictionary {
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation GrowingCustomBuilder

- (void)readPropertyInTrackThread {
[super readPropertyInTrackThread];
GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration;
if (configuration.customEventWithPath) {
_path = _path && _path.length > 0 ? _path : @"/";
}
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmethod-signatures"
#pragma clang diagnostic ignored "-Wmismatched-return-types"
Expand All @@ -55,6 +66,13 @@ @implementation GrowingCustomBuilder
return self;
};
}

- (GrowingBaseBuilder * (^)(NSString *value))setPath {
return ^(NSString *value) {
self->_path = value;
return self;
};
}
#pragma clang diagnostic pop

- (NSString *)eventType {
Expand Down
Loading

0 comments on commit 05a01a8

Please sign in to comment.