Sending after-call events to GA

Learn how to send after-call events to Google Analytics

1) Add this snippet to save GA customer ID:

(Please check that lc_param_cid is filled correctly)
window.leadCM.init_callback_array.push(function() {
    setTimeout(function() {
        try {
            var clientId = ga.getAll()[0].get('clientId');
            if (clientId)
                leadCM.dispatchCustomEvent("CUSTOM_PARAMS", {
                    lc_param_cid: clientId
                });
        } catch (e) {
            console.error(e);
        }
    }, 5000);
});
 

2) Create a server (fill constants with client values):

const CLIENTS_GA_ID = 'UA-123456789-1'; // INSERT HERE CLIENT's GA_ID
const CLIENTS_GA_EVENT_NAME = 'SUCCESSFUL_CALL'; // INSERT HERE CLIENT's TARGET EVENT
leadCM.webserver.post("/webhook", async (req, res) => {
    if (req?.body) {
        const payload = req?.body;
        if (payload.type === 'end_call') {
            console.log(payload);
            if (payload.call_status !== 'answered' && !talk_duration_sec && talk_duration_sec < 30) return;
            if (payload.lead.custom_params.lc_param_cid) {
                const url = 'https://www.google-analytics.com/collect';
                const params = {
                    v: "1",
                    tid: CLIENTS_GA_ID,
                    cid: payload.lead.custom_params.lc_param_cid,
                    t: "event",
                    ec: CLIENTS_GA_EVENT_NAME,
                    z: Math.random().toString(),
                };
                console.log(url, payload);
                const res = await leadCM.httpClient.get(url, {
                    params: params
                });
                console.log(res.data);
            }
        }
    }
    res.send({
        success: true,
        hi: 'there 1',
        seed: Math.random()
    });
});
 

3) Setup the Webhooks for target widgets.

If you face any issues, feel free to drop us an email at support@convolo.ai

 
Did this answer your question?
😞
😐
🤩

Last updated on July 20, 2022