When building a call center with Asterisk/FreePBX and FastAGI (Java), tracking all call events—incoming, ring, answer, end—is critical. Asterisk provides built-in functions, variables, and dialplan commands that make this straightforward.
This article will cover:
- Time & Date Functions
- CDR Variables
- Caller & Channel Info
- Dialplan Functions
- Queue & Agent Variables
- Hangup / Post-call Functions
- Example Dialplan for Call Tracking
- Best Practices
1. Time & Date Functions
Asterisk can provide both epoch time and formatted time, which is crucial for logging and reporting.
| Function / Variable | Purpose | Example |
|---|---|---|
${EPOCH} | Current Unix timestamp (seconds since 1970-01-01 UTC) | Set(INCOMING_EPOCH=${EPOCH}) |
STRFTIME(epoch,[timezone],format) | Convert epoch to human-readable date/time | Set(INCOMING_TIME=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)}) |
${TIME} | Current system time (HH:MM:SS) | NoOp(Time now: ${TIME}) |
${DATE} | Current system date (YYYYMMDD) | NoOp(Date now: ${DATE}) |
${HOUR} | Current hour | NoOp(Hour: ${HOUR}) |
${MINUTE} | Current minute | NoOp(Minute: ${MINUTE}) |
${SECOND} | Current second | NoOp(Second: ${SECOND}) |
Usage: Store these values at different stages—call arrival, agent answer, call end—to log or calculate durations.
2. Call Detail Record (CDR) Variables
CDR is Asterisk’s built-in call logging system. It tracks important metrics:
| Variable | Purpose | Example |
|---|---|---|
${CDR(start)} | Call start timestamp | NoOp(Call start: ${CDR(start)}) |
${CDR(answer)} | Call answer timestamp | NoOp(Call answered: ${CDR(answer)}) |
${CDR(end)} | Call end timestamp | NoOp(Call ended: ${CDR(end)}) |
${CDR(duration)} | Total call duration (seconds) | NoOp(Total duration: ${CDR(duration)}) |
${CDR(billsec)} | Duration from answer to hangup | NoOp(Billable seconds: ${CDR(billsec)}) |
${CDR(disposition)} | Call result (ANSWERED, NO ANSWER, BUSY, FAILED) | NoOp(Result: ${CDR(disposition)}) |
${CDR(userfield)} | Custom field for tracking | Set(CDR(userfield)=SupportQueue) |
Tip: For call center metrics, combine CDR with custom channel variables for detailed reporting.
3. Caller & Channel Information
These variables provide caller identification and channel context:
| Variable | Purpose | Example |
|---|---|---|
${CALLERID(num)} | Caller’s number | NoOp(Caller ID: ${CALLERID(num)}) |
${CALLERID(name)} | Caller’s name | NoOp(Caller Name: ${CALLERID(name)}) |
${CHANNEL} | Current channel name | NoOp(Channel: ${CHANNEL}) |
${UNIQUEID} | Unique call ID | NoOp(Unique call ID: ${UNIQUEID}) |
${EXTEN} | Current extension dialed | NoOp(Extension: ${EXTEN}) |
${CONTEXT} | Current dialplan context | NoOp(Context: ${CONTEXT}) |
${HANGUPCAUSE} | Reason for hangup | NoOp(Hangup cause: ${HANGUPCAUSE}) |
${DIALEDTIME} | Duration of dial attempt (seconds) | NoOp(Dialed duration: ${DIALEDTIME}) |
${ANSWEREDTIME} | Time when channel was answered | NoOp(Answered at: ${ANSWEREDTIME}) |
4. Dialplan Functions / Commands
| Function / Command | Purpose | Example |
|---|---|---|
SET(variable=value) | Set a channel variable | Set(INCOMINGTIME=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)}) |
GET VARIABLE(variable) | Read a variable | NoOp(Caller ID: ${CALLERID(num)}) |
NoOp(text) | Log message to console | NoOp(Incoming call from ${CALLERID(num)}) |
Dial(tech/endpoint,timeout,options) | Connect to agent or endpoint | Dial(SIP/100,30,tT) |
Queue(name[,options]) | Send call to queue | Queue(support,t,,,30) |
AGI(script,args...) | Call AGI/FastAGI script | AGI(log_call_times.agi,${CALLERID(num)},${INCOMINGTIME}) |
Hangup() | End the call | Hangup() |
Gosub(context,exten,priority,args...) | Call another subroutine | Gosub(log_call,h,1,${UNIQUEID}) |
5. Queue & Agent Variables
For call centers using Asterisk Queues:
| Variable / Function | Purpose | Example |
|---|---|---|
${QAGENT} | Agent who answered | NoOp(Agent answered: ${QAGENT}) |
${QUEUENAME} | Queue name | NoOp(Queue: ${QUEUENAME}) |
${QUEUEPOSITION} | Caller’s position in queue | NoOp(Queue position: ${QUEUEPOSITION}) |
${QUEUEWAIT} | Time caller waited in queue | NoOp(Queue wait time: ${QUEUEWAIT}) |
${QUEUECALLERID} | Caller ID in queue | NoOp(Queue caller: ${QUEUECALLERID}) |
Queue() | Send call to queue | Queue(support,t,,,30) |
6. Hangup / Post-Call Functions
To capture call end details:
| Variable / Function | Purpose | Example |
|---|---|---|
h extension | Trigger on hangup | exten => h,1,NoOp(Call ended) |
${HANGUPCAUSE} | Reason for hangup | NoOp(Hangup cause: ${HANGUPCAUSE}) |
${DIALEDTIME} | Dial duration | NoOp(Dial duration: ${DIALEDTIME}) |
${ANSWEREDTIME} | Time call answered | NoOp(Answered at: ${ANSWEREDTIME}) |
AGI(script) | Call AGI/FastAGI for logging | AGI(log_call_times.agi,${CALLERID(num)},${INCOMINGTIME},${ANSWEREDTIME},${ENDTIME}) |
7. Full Example Dialplan for Call Tracking
[call-center]
exten => 100,1,NoOp(Incoming call from ${CALLERID(num)})
same => n,Set(INCOMINGTIME=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)})
same => n,NoOp(Call received at ${INCOMINGTIME})
; Send to queue
same => n,Queue(support,t,,,30)
same => n,Set(ANSWEREDTIME=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)})
same => n,NoOp(Call answered at ${ANSWEREDTIME}, by agent ${QAGENT})
; Hangup extension logs end
exten => h,1,NoOp(Call hangup)
same => n,Set(ENDTIME=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)})
same => n,AGI(log_call_times.agi,${CALLERID(num)},${INCOMINGTIME},${ANSWEREDTIME},${ENDTIME})
Explanation:
- Incoming time captured with
EPOCH→ formatted withSTRFTIME. - Call sent to a queue → after answer, record
ANSWEREDTIME. - Hangup triggers
hextension → recordENDTIME. - FastAGI is used to log all times to database.
8. Best Practices for Call Center Call Tracking
- Use
EPOCHfor precision, store asDATETIMEin DB. - Combine CDR + custom channel variables for metrics like agent answer time.
- Use
hextension for end-of-call logging to capture hangup details. - For queue-based call centers, log
${QAGENT},${QUEUEWAIT},${QUEUEPOSITION}. - Always format times for readability using
STRFTIME. - For Java FastAGI, pass all variables as parameters rather than computing times in Java.
✅ This is a complete reference for all Asterisk functions and variables used in call centers for time tracking, call logging, and queue management.




