Skip to content
Open
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
28 changes: 28 additions & 0 deletions src/ga4mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ga4mp = function (measurement_ids, config = {}) {
beforeLoad: () => {},
beforeRequestSend: () => {},
},
eventCallback: null,
endpoint: 'https://www.google-analytics.com/g/collect',
payloadData: {},
},
Expand Down Expand Up @@ -135,6 +136,21 @@ const ga4mp = function (measurement_ids, config = {}) {
internalModel['persistentEventParameters'][key] = value
}

/**
* Add an events callback, it will be triggered for every track function call
* @param {function} callback
* @returns
*/
const addEventCallback = (callback) => {
if (isFunction(callback)) {
internalModel.eventCallback = callback
} else {
console.error('callback is not a function')
}

}


/**
* setUserProperty
* @param {*} key
Expand Down Expand Up @@ -267,6 +283,17 @@ const ga4mp = function (measurement_ids, config = {}) {
)
}
const payload = buildPayload(eventName, eventParameters, sessionControl)
// run callbacks here

let callbackFn = internalModel.eventCallback;
if(callbackFn !== null) {
try {
callbackFn(internalModel.payloadData.user_id, eventName, eventParameters, internalModel.persistentEventParameters, internalModel.userProperties);
} catch(e) {
console.error('callback failed: '+e)
}
}

if (payload && forceDispatch) {
for (let i = 0; i < payload.tid.length; i++) {
let r = JSON.parse(JSON.stringify(payload))
Expand Down Expand Up @@ -294,6 +321,7 @@ const ga4mp = function (measurement_ids, config = {}) {
setEventsParameter,
setUserId,
trackEvent,
addEventCallback,
}
}

Expand Down