Skip to content
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

[MOB-21131] Display Proposition api to support multiple propositions. #112

Open
wants to merge 16 commits into
base: dev-v5.1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AEPOptimize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
78AF6DBB284FD9AA0022EB24 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 78AF6DB9284FD9AA0022EB24 /* MainInterface.storyboard */; };
78AF6DBF284FD9AA0022EB24 /* AEPOptimizeDemoAppExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 78AF6DB5284FD9AA0022EB24 /* AEPOptimizeDemoAppExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
79AF0BBA3B4B3F0D2B46C847 /* Pods_AEPOptimize.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 89FA9F1584C29AFB176C28B9 /* Pods_AEPOptimize.framework */; };
B65FBE642CA28CD300EC3440 /* Optimize+Tracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = B65FBE632CA28CD300EC3440 /* Optimize+Tracking.swift */; };
B65FBE662CA2E37500EC3440 /* Propositions+OptimizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B65FBE652CA2E37500EC3440 /* Propositions+OptimizeTests.swift */; };
B6B401C52C8052210082B3FE /* OptimizeError.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6B401C42C8052210082B3FE /* OptimizeError.swift */; };
C8076C4D265EE786006BEC5D /* TargetProduct.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8076C3F265EE768006BEC5D /* TargetProduct.swift */; };
C8076C52265EE789006BEC5D /* TargetOrder.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8076C46265EE776006BEC5D /* TargetOrder.swift */; };
Expand Down Expand Up @@ -188,6 +190,8 @@
AB699BC71E1531AB6A02B9AD /* Pods-shared-AEPOptimizeDemoAppExtension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shared-AEPOptimizeDemoAppExtension.debug.xcconfig"; path = "Target Support Files/Pods-shared-AEPOptimizeDemoAppExtension/Pods-shared-AEPOptimizeDemoAppExtension.debug.xcconfig"; sourceTree = "<group>"; };
AB8BAF0E948D2E2747418C4E /* Pods-FunctionalTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FunctionalTests.debug.xcconfig"; path = "Target Support Files/Pods-FunctionalTests/Pods-FunctionalTests.debug.xcconfig"; sourceTree = "<group>"; };
B588A06BD3941420A1C1CBC5 /* Pods-AEPOptimizeDemoObjC.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AEPOptimizeDemoObjC.debug.xcconfig"; path = "Target Support Files/Pods-AEPOptimizeDemoObjC/Pods-AEPOptimizeDemoObjC.debug.xcconfig"; sourceTree = "<group>"; };
B65FBE632CA28CD300EC3440 /* Optimize+Tracking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Optimize+Tracking.swift"; sourceTree = "<group>"; };
B65FBE652CA2E37500EC3440 /* Propositions+OptimizeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Propositions+OptimizeTests.swift"; sourceTree = "<group>"; };
B6B401C42C8052210082B3FE /* OptimizeError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptimizeError.swift; sourceTree = "<group>"; };
BEA1EA2DAE5E666C7DEEA655 /* Pods-UnitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.debug.xcconfig"; path = "Target Support Files/Pods-UnitTests/Pods-UnitTests.debug.xcconfig"; sourceTree = "<group>"; };
C2AB724370848D9EA2ABD4AA /* Pods-AEPOptimizeDemoAppExtension.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AEPOptimizeDemoAppExtension.release.xcconfig"; path = "Target Support Files/Pods-AEPOptimizeDemoAppExtension/Pods-AEPOptimizeDemoAppExtension.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -516,6 +520,7 @@
isa = PBXGroup;
children = (
C88C5F79261D06A5003AE3DE /* Optimize+PublicAPI.swift */,
B65FBE632CA28CD300EC3440 /* Optimize+Tracking.swift */,
C88C5F1126152DAB003AE3DE /* DecisionScope.swift */,
C88C5EDC2614F640003AE3DE /* Offer.swift */,
C8DE6B692684181A0076623F /* Offer+Tracking.swift */,
Expand Down Expand Up @@ -600,6 +605,7 @@
C8CA16DA2624BDA5005B09A0 /* String+OptimizeTests.swift */,
C8CA16DB2624BDA5005B09A0 /* StringInterpolation+OptimizeTests.swift */,
C8227C7627DAD19C005A7703 /* KeyedDecodingContainer+OptimizeTests.swift */,
B65FBE652CA2E37500EC3440 /* Propositions+OptimizeTests.swift */,
);
name = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -1219,6 +1225,7 @@
buildActionMask = 2147483647;
files = (
C88C5EDD2614F640003AE3DE /* Offer.swift in Sources */,
B65FBE642CA28CD300EC3440 /* Optimize+Tracking.swift in Sources */,
C8DE6BEF2686848B0076623F /* Proposition+Tracking.swift in Sources */,
C8227C7527DA5BD6005A7703 /* KeyedDecodingContainer+Optimize.swift in Sources */,
C88C5F3326185EF8003AE3DE /* StringInterpolation+Optimize.swift in Sources */,
Expand Down Expand Up @@ -1246,6 +1253,7 @@
C88C5F0A261523A4003AE3DE /* OfferTests.swift in Sources */,
C8CA16DE2624BDA6005B09A0 /* String+OptimizeTests.swift in Sources */,
C88C5FE3261D8FBF003AE3DE /* MockExtension.swift in Sources */,
B65FBE662CA2E37500EC3440 /* Propositions+OptimizeTests.swift in Sources */,
C88C5F1F2615916D003AE3DE /* DecisionScopeTests.swift in Sources */,
C88C5F56261C3F6A003AE3DE /* OptimizePropositionTests.swift in Sources */,
C88C5F272615A057003AE3DE /* OfferTypeTests.swift in Sources */,
Expand Down
86 changes: 14 additions & 72 deletions Sources/AEPOptimize/Offer+Tracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public extension Offer {
/// - Returns A dictionary containing XDM data for the propositon interactions.
/// - SeeAlso: `interactionXdm(for:)`
func generateDisplayInteractionXdm() -> [String: Any]? {
generateInteractionXdm(for: OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_DISPLAY)
guard let proposition = proposition else {
Log.debug(label: OptimizeConstants.LOG_TAG,
"Cannot send display proposition interaction event for option \(id), proposition reference is not available.")
return nil
}
return Optimize.generateInteractionXdm(for: [proposition], for: OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_DISPLAY)
}

/// Creates a dictionary containing XDM formatted data for `Experience Event - Proposition Interactions` field group from the given proposition option.
Expand All @@ -39,88 +44,25 @@ public extension Offer {
/// - Returns A dictionary containing XDM data for the propositon interactions.
/// - SeeAlso: `interactionXdm(for:)`
func generateTapInteractionXdm() -> [String: Any]? {
generateInteractionXdm(for: OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_INTERACT)
guard let proposition = proposition else {
Log.debug(label: OptimizeConstants.LOG_TAG,
"Cannot send tap proposition interaction event for option \(id), proposition reference is not available.")
return nil
}
return Optimize.generateInteractionXdm(for: [proposition], for: OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_INTERACT)
}

/// Dispatches an event for the Edge extension to send an Experience Event to the Edge network with the display interaction data for the given proposition item.
///
/// - SeeAlso: `trackWithData(_:)`
func displayed() {
trackWithData(generateDisplayInteractionXdm())
Optimize.trackWithData(generateDisplayInteractionXdm())
}

/// Dispatches an event for the Edge extension to send an Experience Event to the Edge network with the tap interaction data for the given proposition item.
///
/// - SeeAlso: `trackWithData(_:)`
func tapped() {
trackWithData(generateTapInteractionXdm())
}

/// Creates a dictionary containing XDM formatted data for `Experience Event - Proposition Interactions` field group from the given proposition option and for the provided event type.
///
/// If the proposition reference within the option is released and no longer valid, the method returns `nil`.
///
/// - Parameter eventType: The Experience Event event type for the proposition interaction.
/// - Returns A dictionary containing XDM data for the propositon interactions.
private func generateInteractionXdm(for eventType: String) -> [String: Any]? {
var propositionDetailsData: [String: Any] = [:]
guard let proposition = proposition else {
Log.debug(label: OptimizeConstants.LOG_TAG,
"Cannot send proposition interaction event (\(eventType)) for option \(id), proposition reference is not available.")
return nil
}

propositionDetailsData = [
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ID: proposition.id,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_SCOPE: proposition.scope,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_SCOPEDETAILS: proposition.scopeDetails,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ITEMS: [
[
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ITEMS_ID: id
]
]
]

var propositionEventType: [String: Any] = [:]
let propEventType = (eventType == OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_DISPLAY) ?
OptimizeConstants.JsonKeys.PROPOSITION_EVENT_TYPE_DISPLAY :
OptimizeConstants.JsonKeys.PROPOSITION_EVENT_TYPE_INTERACT
propositionEventType[propEventType] = 1

let xdmData: [String: Any] = [
OptimizeConstants.JsonKeys.EXPERIENCE_EVENT_TYPE: eventType,
OptimizeConstants.JsonKeys.EXPERIENCE: [
OptimizeConstants.JsonKeys.EXPERIENCE_DECISIONING: [
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITION_EVENT_TYPE: propositionEventType,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS: [propositionDetailsData]
]
]
]
return xdmData
}

/// Dispatches the track propositions request event with type `EventType.optimize` and source `EventSource.requestContent` and given proposition interactions data.
///
/// No event is dispatched if the input xdm data is `nil`.
///
/// - Parameter xdmData: A dictionary containing XDM data for the propositon interactions.
private func trackWithData(_ xdmData: [String: Any]?) {
guard let xdmData = xdmData else {
Log.debug(label: OptimizeConstants.LOG_TAG,
"Cannot send track propositions request event, the provided xdmData is nil.")
return
}

let eventData: [String: Any] = [
OptimizeConstants.EventDataKeys.REQUEST_TYPE: OptimizeConstants.EventDataValues.REQUEST_TYPE_TRACK,
OptimizeConstants.EventDataKeys.PROPOSITION_INTERACTIONS: xdmData
]

let event = Event(name: OptimizeConstants.EventNames.TRACK_PROPOSITIONS_REQUEST,
type: EventType.optimize,
source: EventSource.requestContent,
data: eventData)

MobileCore.dispatch(event: event)
Optimize.trackWithData(generateTapInteractionXdm())
}
}
90 changes: 90 additions & 0 deletions Sources/AEPOptimize/Optimize+Tracking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import AEPCore
import AEPServices
import Foundation

@objc
public extension Optimize {
/// This API dispatches an event for the Edge extension to send an Experience Event to the Edge network with the display interaction data for list of propositions passed.
///
/// - Parameter propositions: An array of optimize propositions.
static func trackDisplayedPropositions(for propositions: [OptimizeProposition]) {
let xdm = generateInteractionXdm(for: propositions, for: OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_DISPLAY)
trackWithData(xdm)
}

/// Creates a dictionary containing XDM formatted data for `Experience Event - Proposition Interactions` field group from the given list of propositions and for the provided event type.
///
/// - Parameter propositions: An array of optimize propositions.
/// - Parameter eventType: The Experience Event event type for the proposition interaction.
/// - Returns A dictionary containing XDM data for the propositon interactions.
static func generateInteractionXdm(for propositions: [OptimizeProposition], for eventType: String) -> [String: Any]? {
var propositionDetailsData: [[String: Any]] = []

for proposition in propositions {
let propositionMap: [String: Any] = [
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ID: proposition.id,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_SCOPE: proposition.scope,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_SCOPEDETAILS: proposition.scopeDetails,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ITEMS: [
[
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS_ITEMS_ID: proposition.offers[0].id
]
]
]
propositionDetailsData.append(propositionMap)
}

var propositionEventType: [String: Any] = [:]
let propEventType = (eventType == OptimizeConstants.JsonValues.EE_EVENT_TYPE_PROPOSITION_DISPLAY) ?
OptimizeConstants.JsonKeys.PROPOSITION_EVENT_TYPE_DISPLAY :
OptimizeConstants.JsonKeys.PROPOSITION_EVENT_TYPE_INTERACT
propositionEventType[propEventType] = 1

let xdmData: [String: Any] = [
OptimizeConstants.JsonKeys.EXPERIENCE_EVENT_TYPE: eventType,
OptimizeConstants.JsonKeys.EXPERIENCE: [
OptimizeConstants.JsonKeys.EXPERIENCE_DECISIONING: [
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITION_EVENT_TYPE: propositionEventType,
OptimizeConstants.JsonKeys.DECISIONING_PROPOSITIONS: propositionDetailsData
]
]
]
return xdmData
}

/// Dispatches the track propositions request event with type `EventType.optimize` and source `EventSource.requestContent` and given proposition interactions data.
///
/// No event is dispatched if the input xdm data is `nil`.
///
/// - Parameter xdmData: A dictionary containing XDM data for the propositon interactions.
static func trackWithData(_ xdmData: [String: Any]?) {
guard let xdmData = xdmData else {
Log.debug(label: OptimizeConstants.LOG_TAG,
"Cannot send track propositions request event, the provided xdmData is nil.")
return
}

let eventData: [String: Any] = [
OptimizeConstants.EventDataKeys.REQUEST_TYPE: OptimizeConstants.EventDataValues.REQUEST_TYPE_TRACK,
OptimizeConstants.EventDataKeys.PROPOSITION_INTERACTIONS: xdmData
]

let event = Event(name: OptimizeConstants.EventNames.TRACK_PROPOSITIONS_REQUEST,
type: EventType.optimize,
source: EventSource.requestContent,
data: eventData)

MobileCore.dispatch(event: event)
}
}
Loading