Skip to content

Commit

Permalink
Merge pull request #111 from vespinola/feature/APP-3103
Browse files Browse the repository at this point in the history
[APP-3103] Add request url to SDK framework
  • Loading branch information
Vladimir Espinola Lezcano authored Jul 12, 2024
2 parents 91dc14c + b717c3b commit 1116ad1
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 62 deletions.
111 changes: 69 additions & 42 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -157,48 +157,78 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="ProductImage" translatesAutoresizingMaskIntoConstraints="NO" id="9xR-K8-uf1">
<rect key="frame" x="87" y="144" width="240" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" text="T-Shirt" textAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="vq5-I3-w6P">
<rect key="frame" x="164" y="280" width="86" height="37"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oAv-pI-GrN">
<rect key="frame" x="88" y="325" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart"/>
<connections>
<action selector="addToCartButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="wpQ-qK-0S1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oiK-SR-ICR">
<rect key="frame" x="87" y="412" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Purchase"/>
<connections>
<action selector="purchaseButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="ZGF-RD-AUi"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oL8-3x-dTc">
<rect key="frame" x="87" y="500" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Send Custom Event"/>
<connections>
<action selector="customEventButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="CaV-GA-xkL"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillProportionally" spacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="pId-os-gqS">
<rect key="frame" x="36" y="78" width="342" height="476"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillProportionally" alignment="center" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="b6b-XP-epa">
<rect key="frame" x="0.0" y="0.0" width="342" height="160"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="ProductImage" translatesAutoresizingMaskIntoConstraints="NO" id="9xR-K8-uf1">
<rect key="frame" x="110" y="0.0" width="122" height="129.5"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="T-Shirt" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rcE-kP-CJk">
<rect key="frame" x="145" y="137.5" width="52.5" height="22.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="160" id="EWh-qg-xc9"/>
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="Zev-3g-6U9">
<rect key="frame" x="0.0" y="176" width="342" height="300"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oAv-pI-GrN">
<rect key="frame" x="0.0" y="0.0" width="342" height="63"/>
<constraints>
<constraint firstAttribute="height" constant="63" id="wXo-ak-iSl"/>
</constraints>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart"/>
<connections>
<action selector="addToCartButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="wpQ-qK-0S1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oiK-SR-ICR">
<rect key="frame" x="0.0" y="79" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Purchase"/>
<connections>
<action selector="purchaseButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="ZGF-RD-AUi"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oL8-3x-dTc">
<rect key="frame" x="0.0" y="158" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Send Custom Event"/>
<connections>
<action selector="customEventButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="CaV-GA-xkL"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="x1u-jl-cmP" userLabel="Add To Cart with Deeplink">
<rect key="frame" x="0.0" y="237" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart with Deeplink"/>
<connections>
<action selector="addToCartWithDeeplinkButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="06j-W6-5HX"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
<viewLayoutGuide key="safeArea" id="KyE-6k-rz2"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="KyE-6k-rz2" firstAttribute="trailing" secondItem="pId-os-gqS" secondAttribute="trailing" constant="36" id="3qv-hS-kpv"/>
<constraint firstItem="pId-os-gqS" firstAttribute="centerX" secondItem="KyE-6k-rz2" secondAttribute="centerX" id="8Wb-r3-0QI"/>
<constraint firstItem="pId-os-gqS" firstAttribute="top" secondItem="KyE-6k-rz2" secondAttribute="top" constant="30" id="Bzl-W3-Wz1"/>
<constraint firstItem="KyE-6k-rz2" firstAttribute="bottom" secondItem="pId-os-gqS" secondAttribute="bottom" priority="999" id="ILO-JJ-ljv"/>
<constraint firstItem="pId-os-gqS" firstAttribute="leading" secondItem="KyE-6k-rz2" secondAttribute="leading" constant="36" id="MJ3-FE-55x"/>
</constraints>
</view>
<tabBarItem key="tabBarItem" title="Product" image="cart.fill" catalog="system" id="Kez-RS-Jz1"/>
<navigationItem key="navigationItem" id="gu9-Nv-e2w"/>
Expand All @@ -217,9 +247,6 @@
<image name="ProductImage" width="122" height="122"/>
<image name="cart.fill" catalog="system" width="128" height="102"/>
<image name="house.fill" catalog="system" width="128" height="104"/>
<systemColor name="labelColor">
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
Expand Down
32 changes: 17 additions & 15 deletions Example/Example/ProductViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ @interface ProductViewController ()
@end

@implementation ProductViewController

- (void)viewDidLoad {
[super viewDidLoad];
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

ATTNItem* item = [self buildItem];
ATTNProductViewEvent* productView = [[ATTNProductViewEvent alloc] initWithItems:@[ item ]];
productView.deeplink = @"https://mydeeplink.com/products/32432423";

[[ATTNEventTracker sharedInstance] recordEvent:productView];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

[self showToast:@"Product View event sent"];
[self showToast:@"Product View event sent" duration:2];
}


Expand All @@ -35,7 +31,16 @@ - (IBAction)addToCartButtonPressed:(id)sender {
ATTNAddToCartEvent* addToCart = [[ATTNAddToCartEvent alloc] initWithItems:@[ item ]];

[[ATTNEventTracker sharedInstance] recordEvent:addToCart];
[self showToast:@"Add To Cart event sent"];
[self showToast:@"Add To Cart event sent" duration:2];
}

- (IBAction)addToCartWithDeeplinkButtonPressed:(id)sender {
ATTNItem* item = [self buildItem];
ATTNAddToCartEvent* addToCart = [[ATTNAddToCartEvent alloc] initWithItems:@[ item ]];
addToCart.deeplink = @"https://mydeeplink.com/products/32432423";

[[ATTNEventTracker sharedInstance] recordEvent:addToCart];
[self showToast: [NSString stringWithFormat:@"Add To Cart event sent with requestURL(pd): '%@'", addToCart.deeplink] duration:4];
}

- (IBAction)purchaseButtonPressed:(id)sender {
Expand All @@ -50,7 +55,7 @@ - (IBAction)purchaseButtonPressed:(id)sender {

[[ATTNEventTracker sharedInstance] recordEvent:purchase];

[self showToast:@"Purchase event sent"];
[self showToast:@"Purchase event sent" duration:2];
}

- (ATTNItem*)buildItem {
Expand All @@ -67,19 +72,16 @@ - (IBAction)customEventButtonPressed:(id)sender {

[[ATTNEventTracker sharedInstance] recordEvent:customEvent];

[self showToast:@"Custom event sent"];
[self showToast:@"Custom event sent" duration:2];
}


- (void)showToast:(NSString*)message {
- (void)showToast:(NSString*)message duration:(int)duration {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];

[self presentViewController:alert animated:YES completion:nil];

int duration = 1; // duration in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
Expand Down
1 change: 1 addition & 0 deletions Sources/API/ATTNEventRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
final class ATTNEventRequest {
var metadata: [String: Any]
let eventNameAbbreviation: String
var deeplink: String?

init(metadata: [String: Any], eventNameAbbreviation: String) {
self.metadata = metadata
Expand Down
12 changes: 11 additions & 1 deletion Sources/Helpers/Extension/ATTNAddToCartEvent+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ extension ATTNAddToCartEvent: ATTNEventRequestProvider {
for item in items {
var metadata = [String: Any]()
item.addItem(toDictionary: &metadata, with: priceFormatter)
eventRequests.append(.init(metadata: metadata, eventNameAbbreviation: ATTNEventTypes.addToCart))

let eventRequest = ATTNEventRequest(
metadata: metadata,
eventNameAbbreviation: ATTNEventTypes.addToCart
)

if let deeplink {
eventRequest.deeplink = deeplink
}

eventRequests.append(eventRequest)
}

return eventRequests
Expand Down
12 changes: 11 additions & 1 deletion Sources/Helpers/Extension/ATTNProductViewEvent+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ extension ATTNProductViewEvent: ATTNEventRequestProvider {
for item in items {
var metadata = [String: Any]()
item.addItem(toDictionary: &metadata, with: priceFormatter)
eventRequests.append(.init(metadata: metadata, eventNameAbbreviation: ATTNEventTypes.productView))

let eventRequest = ATTNEventRequest(
metadata: metadata,
eventNameAbbreviation: ATTNEventTypes.productView
)

if let deeplink {
eventRequest.deeplink = deeplink
}

eventRequests.append(eventRequest)
}

return eventRequests
Expand Down
13 changes: 11 additions & 2 deletions Sources/Public/Events/ATTNAddToCartEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ import Foundation
@objc(ATTNAddToCartEvent)
public final class ATTNAddToCartEvent: NSObject, ATTNEvent {
@objc public let items: [ATTNItem]
@objc public var deeplink: String?

@objc
public init(items: [ATTNItem]) {
@objc(initWithItems:)
public convenience init(items: [ATTNItem]) {
self.init(items: items, deeplink: nil)
}

@objc(initWithItems:deeplink:)
public init(items: [ATTNItem], deeplink: String?) {
self.items = items
self.deeplink = deeplink
super.init()
}

private override init() {
fatalError("init() has not been implemented")
}
}

extension ATTNAddToCartEvent: ATTNDeeplinkHandling { }
12 changes: 12 additions & 0 deletions Sources/Public/Events/ATTNDeeplinkHandling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ATTNDeeplinkHandling.swift
// attentive-ios-sdk-framework
//
// Created by Vladimir - Work on 2024-07-10.
//

import Foundation

protocol ATTNDeeplinkHandling {
var deeplink: String? { get set }
}
11 changes: 10 additions & 1 deletion Sources/Public/Events/ATTNProductViewEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ import Foundation
@objc(ATTNProductViewEvent)
public final class ATTNProductViewEvent: NSObject, ATTNEvent {
@objc public let items: [ATTNItem]
@objc public var deeplink: String?

@objc(initWithItems:)
public init(items: [ATTNItem]) {
public convenience init(items: [ATTNItem]) {
self.init(items: items, deeplink: nil)
}

@objc(initWithItems:deeplink:)
public init(items: [ATTNItem], deeplink: String?) {
self.items = items
self.deeplink = deeplink
super.init()
}

private override init() {
fatalError("init() has not been implemented")
}
}

extension ATTNProductViewEvent: ATTNDeeplinkHandling { }
4 changes: 4 additions & 0 deletions Sources/URLProviders/ATTNEventURLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ struct ATTNEventURLProvider: ATTNEventURLProviding {
queryParams["m"] = try? ATTNJsonUtils.convertObjectToJson(combinedMetadata) ?? "{}"
queryParams["t"] = eventRequest.eventNameAbbreviation

if let deeplink = eventRequest.deeplink {
queryParams["pd"] = deeplink
}

urlComponents.queryItems = queryParams.map { .init(name: $0.key, value: $0.value) }
return urlComponents.url
}
Expand Down
Loading

0 comments on commit 1116ad1

Please sign in to comment.