Skip to content

Commit

Permalink
feat: page attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Dec 14, 2022
1 parent 11c5395 commit acece74
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 568 deletions.
11 changes: 5 additions & 6 deletions Example/Example/UICategoryTests/Hybrid/gio_hybrideventtest.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,20 @@
sendMockEvent(event);
}

function sendMockPageAttributesEvent() {
function sendMockPageEventWithAttributes() {
let event = {
"deviceId": "7196f014-d7bc-4bd8-b920-757cb2375ff6",
"sessionId": "d5cbcf77-b38b-4223-954f-c6a2fdc0c098",
"eventType": "PAGE_ATTRIBUTES",
"eventType": "PAGE",
"platform": "Web",
"timestamp": 1602485628504,
"domain": "test-browser.growingio.com",
"path": "/push/web.html",
"query": "a=1&b=2",
"title": "Hybrid测试页面",
"referralPage": "http://test-browser.growingio.com/push",
"protocolType": "https",
"globalSequenceId": 99,
"eventSequenceId": 3,
"pageShowTimestamp": 1602485626878,
"attributes": {
"key1": "value1",
"key2": "value2",
Expand Down Expand Up @@ -381,8 +380,8 @@ <h3>这仅仅是一个测试页面</h3>
<button class="button" onclick="javascript:sendMockFilePageEvent()" type="button">
sendMockFilePageEvent
</button>
<button class="button" onclick="javascript:sendMockPageAttributesEvent()" type="button">
sendMockPageAttributesEvent
<button class="button" onclick="javascript:sendMockPageEventWithAttributes()" type="button">
sendMockPageEventWithAttributes
</button>
<button class="button" onclick="javascript:sendMockViewClickEvent()" type="button">
sendMockViewClickEvent
Expand Down
2 changes: 0 additions & 2 deletions Example/GrowingAnalyticsTests/Helper/ManualTrackHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

+ (BOOL)pageEventCheck:(NSDictionary *)event;

+ (BOOL)pageAttributesEventCheck:(NSDictionary *)event;

+ (BOOL)viewClickEventCheck:(NSDictionary *)event;

+ (BOOL)viewChangeEventCheck:(NSDictionary *)event;
Expand Down
11 changes: 1 addition & 10 deletions Example/GrowingAnalyticsTests/Helper/ManualTrackHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,7 @@ + (BOOL)pageEventCheck:(NSDictionary *)event {
return NO;
}
NSArray *protocol = [self.context arrayByAddingObjectsFromArray:@[@"path", @"orientation"]];
NSArray *optional = @[@"title", @"referralPage", @"query", @"protocolType"];
return [self protocolCheck:event protocol:protocol] && [self emptyPropertyCheck:event optional:optional];
}

+ (BOOL)pageAttributesEventCheck:(NSDictionary *)event {
if (event.count == 0) {
return NO;
}
NSArray *protocol = [self.context arrayByAddingObjectsFromArray:@[@"path", @"pageShowTimestamp", @"attributes"]];
NSArray *optional = @[@"query"];
NSArray *optional = @[@"title", @"referralPage", @"query", @"protocolType", @"attributes"];
return [self protocolCheck:event protocol:protocol] && [self emptyPropertyCheck:event optional:optional];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#import "Modules/Hybrid/Events/GrowingHybridViewElementEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridCustomEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageAttributesEvent.h"
#import "ManualTrackHelper.h"
#import "GrowingTrackerCore/Thread/GrowingDispatchManager.h"
#import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h"
Expand Down Expand Up @@ -197,23 +196,26 @@ - (void)test07SendMockPageEventWithQuery {
XCTAssertEqualObjects(dic[@"query"], @"a=1&b=2");
}

- (void)test08SendMockPageAttributesEvent {
- (void)test08SendMockPageEventWithAttributes {
KIFUIViewTestActor *actor = [viewTester usingLabel:@"HybridWebView"];
[self webView:actor.view evaluateJavaScript:@"sendMockPageAttributesEvent()"];
[self webView:actor.view evaluateJavaScript:@"sendMockPageEventWithAttributes()"];
[viewTester waitForTimeInterval:1];

NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypePageAttributes];
NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypePage];
XCTAssertEqual(events.count, 1);

GrowingHybridPageAttributesEvent *event = (GrowingHybridPageAttributesEvent *)events.firstObject;
GrowingHybridPageEvent *event = (GrowingHybridPageEvent *)events.firstObject;
NSDictionary *dic = event.toDictionary;
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypePageAttributes);
XCTAssertTrue([ManualTrackHelper pageAttributesEventCheck:dic]);
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypePage);
XCTAssertTrue([ManualTrackHelper pageEventCheck:dic]);
XCTAssertTrue([ManualTrackHelper contextOptionalPropertyCheck:dic]);

XCTAssertEqualObjects(dic[@"domain"], @"test-browser.growingio.com");
XCTAssertEqualObjects(dic[@"path"], @"/push/web.html");
XCTAssertEqualObjects(dic[@"query"], @"a=1&b=2");
XCTAssertEqualObjects(dic[@"title"], @"Hybrid测试页面");
XCTAssertNotNil(dic[@"attributes"]);
XCTAssertEqualObjects(dic[@"attributes"][@"key1"], @"value1");
XCTAssertEqualObjects(dic[@"attributes"][@"key2"], @"value2");
}

- (void)test09SendMockViewClickEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#import <XCTest/XCTest.h>

#import "Modules/Hybrid/Events/GrowingHybridCustomEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageAttributesEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridViewElementEvent.h"

Expand Down Expand Up @@ -54,25 +53,15 @@ - (void)testGrowingHybridCustomEvent {
XCTAssertTrue([value isEqualToString:@"value"]);
}

- (void)testGrowingHybridPageAttributesEvent {
GrowingHybridPageAttributesEvent *event = (GrowingHybridPageAttributesEvent *)GrowingHybridPageAttributesEvent.builder.setPath(@"/hybrid/test")
.setQuery(@"testquery")
.setPageShowTimestamp(123456677)
.setAttributes(@{@"test":@"value"}).build;

XCTAssertEqual(event.path, @"/hybrid/test");
XCTAssertEqual(event.query, @"testquery");
NSString *value = (NSString *)event.attributes[@"test"];
XCTAssertTrue([value isEqualToString:@"value"]);
}

- (void)testGrowingHybridPageEvent {
GrowingHybridPageEvent *event = (GrowingHybridPageEvent *)GrowingHybridPageEvent.builder
.setQuery(@"testquery")
.setProtocolType(@"testProtocol").build;
.setProtocolType(@"testProtocol")
.setAttributes(@{@"test":@"value"}).build;

XCTAssertEqual(event.query, @"testquery");
XCTAssertEqual(event.protocolType, @"testProtocol");
XCTAssertEqualObjects(event.attributes[@"test"], @"value");
}

- (void)testGrowingPageCustomEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import "GrowingTrackerCore/Thread/GrowingDispatchManager.h"
#import "Modules/Hybrid/GrowingHybridBridgeProvider.h"
#import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h"
#import "Modules/Hybrid/Events/GrowingHybridPageAttributesEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageEvent.h"
#import "GrowingTrackerCore/Helpers/NSDictionary+GrowingHelper.h"
#import "GrowingTrackerCore/Helpers/NSString+GrowingHelper.h"
#import "GrowingTrackerCore/Manager/GrowingSession.h"
Expand Down Expand Up @@ -66,9 +66,8 @@ - (void)setUp {
- (void)testGrowingHybridBridgeProvider {
[self.provider handleJavascriptBridgeMessage:@"testHibrid"];

GrowingBaseBuilder *builder = GrowingHybridPageAttributesEvent.builder.setQuery(@"QUERY")
GrowingBaseBuilder *builder = GrowingHybridPageEvent.builder.setQuery(@"QUERY")
.setPath(@"KEY_PATH")
.setPageShowTimestamp(123456)
.setAttributes(@{@"test" : @"value"})
.setDomain(@"domain")
.setUserId(@"testUserId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#import "GrowingTrackerCore/Event/GrowingAppCloseEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageAttributesEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridPageAttributesEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingViewElementEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridViewElementEvent.h"
#import "Modules/Hybrid/Events/GrowingHybridEventType.h"
Expand Down Expand Up @@ -271,6 +269,7 @@ - (void)testEventConvertToPB_Page {
.setOrientation(@"PORTRAIT")
.setTitle(@"title")
.setReferralPage(@"referralPage")
.setAttributes(@{@"key" : @"value"})
.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
Expand All @@ -280,6 +279,7 @@ - (void)testEventConvertToPB_Page {
XCTAssertEqualObjects(event.orientation ?: @"", protobuf.orientation);
XCTAssertEqualObjects(event.title ?: @"", protobuf.title);
XCTAssertEqualObjects(event.referralPage ?: @"", protobuf.referralPage);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
{
GrowingPageEvent *event = (GrowingPageEvent *)(GrowingPageEvent.builder.build);
Expand All @@ -291,6 +291,7 @@ - (void)testEventConvertToPB_Page {
XCTAssertEqualObjects(event.orientation ?: @"", protobuf.orientation);
XCTAssertEqualObjects(event.title ?: @"", protobuf.title);
XCTAssertEqualObjects(event.referralPage ?: @"", protobuf.referralPage);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}

// GrowingHybridPageEvent
Expand All @@ -302,6 +303,7 @@ - (void)testEventConvertToPB_Page {
.setReferralPage(@"referralPage")
.setQuery(@"query")
.setProtocolType(@"https")
.setAttributes(@{@"key" : @"value"})
.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
Expand All @@ -313,6 +315,7 @@ - (void)testEventConvertToPB_Page {
XCTAssertEqualObjects(event.referralPage ?: @"", protobuf.referralPage);
XCTAssertEqualObjects(event.query ?: @"", protobuf.query);
XCTAssertEqualObjects(event.protocolType ?: @"", protobuf.protocolType);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
{
GrowingHybridPageEvent *event = (GrowingHybridPageEvent *)(GrowingHybridPageEvent.builder.build);
Expand All @@ -326,65 +329,8 @@ - (void)testEventConvertToPB_Page {
XCTAssertEqualObjects(event.referralPage ?: @"", protobuf.referralPage);
XCTAssertEqualObjects(event.query ?: @"", protobuf.query);
XCTAssertEqualObjects(event.protocolType ?: @"", protobuf.protocolType);
}
}

- (void)testEventConvertToPB_PageAttributes {
// GrowingPageAttributesEvent
{
GrowingPageAttributesEvent *event = (GrowingPageAttributesEvent *)(GrowingPageAttributesEvent.builder
.setPath(@"path")
.setPageShowTimestamp(1638857558209)
.setAttributes(@{@"key": @"value"})
.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypePageAttributes, event.eventType);
XCTAssertEqual(GrowingPBEventType_PageAttributes, protobuf.eventType);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqual(event.pageShowTimestamp, protobuf.pageShowTimestamp);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}
{
GrowingPageAttributesEvent *event = (GrowingPageAttributesEvent *)(GrowingPageAttributesEvent.builder.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypePageAttributes, event.eventType);
XCTAssertEqual(GrowingPBEventType_PageAttributes, protobuf.eventType);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqual(event.pageShowTimestamp, protobuf.pageShowTimestamp);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
}

// GrowingHybridPageAttributesEvent
{
GrowingHybridPageAttributesEvent *event = (GrowingHybridPageAttributesEvent *)(GrowingHybridPageAttributesEvent.builder
.setQuery(@"query")
.setPath(@"path")
.setPageShowTimestamp(1638857558209)
.setAttributes(@{@"key": @"value"})
.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypePageAttributes, event.eventType);
XCTAssertEqual(GrowingPBEventType_PageAttributes, protobuf.eventType);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqual(event.pageShowTimestamp, protobuf.pageShowTimestamp);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
XCTAssertEqualObjects(event.query ?: @"", protobuf.query);
}
{
GrowingHybridPageAttributesEvent *event = (GrowingHybridPageAttributesEvent *)(GrowingHybridPageAttributesEvent.builder
.build);
GrowingPBEventV3Dto *protobuf = [self protobufFromEvent:event];
[self contrastOfDefaultParamter:event protobuf:protobuf];
XCTAssertEqualObjects(GrowingEventTypePageAttributes, event.eventType);
XCTAssertEqual(GrowingPBEventType_PageAttributes, protobuf.eventType);
XCTAssertEqualObjects(event.path ?: @"", protobuf.path);
XCTAssertEqual(event.pageShowTimestamp, protobuf.pageShowTimestamp);
XCTAssertEqualObjects(event.attributes ?: @{}, protobuf.attributes);
XCTAssertEqualObjects(event.query ?: @"", protobuf.query);
}
}

- (void)testEventConvertToPB_ViewClick {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#import "GrowingTrackerCore/Event/GrowingVisitorAttributesEvent.h"
#import "GrowingTrackerCore/Event/GrowingConversionVariableEvent.h"
#import "GrowingTrackerCore/Event/GrowingLoginUserAttributesEvent.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageAttributesEvent.h"

@interface EventTest : XCTestCase

Expand Down Expand Up @@ -207,7 +206,8 @@ - (void)testGrowingPageEvent {
.setPath(@"path")
.setOrientation(orientation)
.setTitle(@"title")
.setReferralPage(@"referralPage");
.setReferralPage(@"referralPage")
.setAttributes(@{@"key" : @"value"});
[GrowingEventManager.sharedInstance postEventBuilder:builder];

// !!! 注意:这里有个隐藏的死锁问题 !!!
Expand All @@ -226,13 +226,15 @@ - (void)testGrowingPageEvent {
XCTAssertEqualObjects(event.orientation, orientation);
XCTAssertEqualObjects(event.title, @"title");
XCTAssertEqualObjects(event.referralPage, @"referralPage");
XCTAssertEqualObjects(event.attributes[@"key"], @"value");

NSDictionary *dic = event.toDictionary;
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypePage);
XCTAssertEqualObjects(dic[@"path"], @"path");
XCTAssertEqualObjects(dic[@"orientation"], orientation);
XCTAssertEqualObjects(dic[@"title"], @"title");
XCTAssertEqualObjects(dic[@"referralPage"], @"referralPage");
XCTAssertEqualObjects(dic[@"attributes"][@"key"], @"value");

NSMutableDictionary *mDic = dic.mutableCopy;
if (dic[@"orientation"] == nil && orientation == nil) {
Expand All @@ -248,31 +250,6 @@ - (void)testGrowingPageEvent {
[self waitForExpectationsWithTimeout:3.0f handler:nil];
}

- (void)testGrowingPageAttributesEvent {
GrowingBaseBuilder *builder = GrowingPageAttributesEvent.builder
.setPath(@"path")
.setPageShowTimestamp(1638857558209)
.setAttributes(@{@"key" : @"value"});
[GrowingEventManager.sharedInstance postEventBuilder:builder];

NSArray<GrowingBaseEvent *> *events = [MockEventQueue.sharedQueue eventsFor:GrowingEventTypePageAttributes];
XCTAssertEqual(events.count, 1);

GrowingPageAttributesEvent *event = (GrowingPageAttributesEvent *)events.firstObject;
XCTAssertEqualObjects(event.eventType, GrowingEventTypePageAttributes);
XCTAssertEqualObjects(event.path, @"path");
XCTAssertEqual(event.pageShowTimestamp, 1638857558209);
XCTAssertEqualObjects(event.attributes[@"key"], @"value");

NSDictionary *dic = event.toDictionary;
XCTAssertEqualObjects(dic[@"eventType"], GrowingEventTypePageAttributes);
XCTAssertEqualObjects(dic[@"path"], @"path");
XCTAssertEqualObjects(dic[@"pageShowTimestamp"], @(1638857558209));
XCTAssertEqualObjects(dic[@"attributes"][@"key"], @"value");
XCTAssertTrue([ManualTrackHelper pageAttributesEventCheck:dic]);
XCTAssertTrue([ManualTrackHelper contextOptionalPropertyCheck:dic]);
}

#pragma mark - Private Methods

- (NSString *)deviceOrientation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GrowingTrackerCore/Event/GrowingEventManager.h"
#import "GrowingTrackerCore/Event/Autotrack/GrowingPageEvent.h"
#import "GrowingAutotrackerCore/Page/GrowingPage.h"
#import "GrowingAutotrackerCore/Page/GrowingPageManager.h"
#import "GrowingAutotrackerCore/Private/GrowingPrivateCategory.h"
#import "GrowingAutotrackerCore/Autotrack/UIViewController+GrowingAutotracker.h"
#import "GrowingAutotrackerCore/GrowingNode/Category/UIViewController+GrowingNode.h"
#import "GrowingAutotrackerCore/Private/GrowingPrivateCategory.h"
#import "GrowingULViewControllerLifecycle.h"

@implementation UIViewController (GrowingAutotracker)
Expand Down
Loading

0 comments on commit acece74

Please sign in to comment.