@@ -20,17 +20,22 @@ const EXTRACTION_STRATEGIES = {
2020 googleanalytics : extractGoogleAnalyticsEvent ,
2121 snowplow : extractSnowplowEvent ,
2222 mparticle : extractMparticleEvent ,
23+ custom : extractCustomEvent ,
2324 default : extractDefaultEvent
2425} ;
2526
2627/**
2728 * Extracts event information from a CallExpression node
2829 * @param {Object } node - AST CallExpression node
2930 * @param {string } source - Analytics provider source
31+ * @param {Object } customConfig - Parsed custom function configuration
3032 * @returns {EventData } Extracted event data
3133 */
32- function extractEventData ( node , source ) {
34+ function extractEventData ( node , source , customConfig ) {
3335 const strategy = EXTRACTION_STRATEGIES [ source ] || EXTRACTION_STRATEGIES . default ;
36+ if ( source === 'custom' ) {
37+ return strategy ( node , customConfig ) ;
38+ }
3439 return strategy ( node ) ;
3540}
3641
@@ -113,16 +118,41 @@ function extractDefaultEvent(node) {
113118 return { eventName, propertiesNode } ;
114119}
115120
121+ /**
122+ * Extracts Custom function event data according to signature
123+ * @param {Object } node - CallExpression node
124+ * @param {Object } customConfig - Parsed custom function configuration
125+ * @returns {EventData & {extraArgs:Object} } event data plus extra args map
126+ */
127+ function extractCustomEvent ( node , customConfig ) {
128+ const args = node . arguments || [ ] ;
129+
130+ const eventArg = args [ customConfig ?. eventIndex ?? 0 ] ;
131+ const propertiesArg = args [ customConfig ?. propertiesIndex ?? 1 ] ;
132+
133+ const eventName = getStringValue ( eventArg ) ;
134+
135+ const extraArgs = { } ;
136+ if ( customConfig && customConfig . extraParams ) {
137+ customConfig . extraParams . forEach ( extra => {
138+ extraArgs [ extra . name ] = args [ extra . idx ] ;
139+ } ) ;
140+ }
141+
142+ return { eventName, propertiesNode : propertiesArg , extraArgs } ;
143+ }
144+
116145/**
117146 * Processes extracted event data into final event object
118147 * @param {EventData } eventData - Raw event data
119148 * @param {string } source - Analytics source
120149 * @param {string } filePath - File path
121150 * @param {number } line - Line number
122151 * @param {string } functionName - Containing function name
152+ * @param {Object } customConfig - Parsed custom function configuration
123153 * @returns {Object|null } Processed event object or null
124154 */
125- function processEventData ( eventData , source , filePath , line , functionName ) {
155+ function processEventData ( eventData , source , filePath , line , functionName , customConfig ) {
126156 const { eventName, propertiesNode } = eventData ;
127157
128158 if ( ! eventName || ! propertiesNode || propertiesNode . type !== NODE_TYPES . OBJECT_EXPRESSION ) {
@@ -131,6 +161,15 @@ function processEventData(eventData, source, filePath, line, functionName) {
131161
132162 let properties = extractProperties ( propertiesNode ) ;
133163
164+ // Handle custom extra params
165+ if ( source === 'custom' && customConfig && eventData . extraArgs ) {
166+ for ( const [ paramName , argNode ] of Object . entries ( eventData . extraArgs ) ) {
167+ properties [ paramName ] = {
168+ type : inferNodeValueType ( argNode )
169+ } ;
170+ }
171+ }
172+
134173 // Special handling for Snowplow: remove 'action' from properties
135174 if ( source === 'snowplow' && properties . action ) {
136175 delete properties . action ;
@@ -173,6 +212,25 @@ function findPropertyByKey(objectNode, key) {
173212 ) ;
174213}
175214
215+ /**
216+ * Infers the type of a value from an AST node (simple heuristic)
217+ * @param {Object } node - AST node
218+ * @returns {string } inferred type
219+ */
220+ function inferNodeValueType ( node ) {
221+ if ( ! node ) return 'any' ;
222+ switch ( node . type ) {
223+ case NODE_TYPES . LITERAL :
224+ return typeof node . value ;
225+ case NODE_TYPES . OBJECT_EXPRESSION :
226+ return 'object' ;
227+ case NODE_TYPES . ARRAY_EXPRESSION :
228+ return 'array' ;
229+ default :
230+ return 'any' ;
231+ }
232+ }
233+
176234module . exports = {
177235 extractEventData,
178236 processEventData
0 commit comments