Tracking marketing calls in Google Analytics from FreePBX

If you track your website traffic and metrics you already know the importance of capturing and analyzing this data.  These metrics allow you to see trends over time, help guide decisions about your website, and track goals and conversions.  Someone fills out your contact form?  GREAT!  That’s a new lead!

What the below integration intends to do is to add the tracking of inbound calls as a conversion within Google Analytics (GA).  Sure, you could set up a ‘click-to-call’ button that could be captured via GA.  What if someone just dials your number, though?  Shouldn’t that count as a conversion?  We’ll bet your SEO people think so!  This is a great way to add more metrics for calculating the ROI on your landing pages and marketing efforts.  Let’s get started!

Important Info

This guide is for UA Google Analytics properties and not the new Google Analytics 4 properties.  The new Google Analytics 4 properties have different methods for event notifications that are out of scope for this post.  If you have questions about what you are using, speak to your trusted SEO specialist(s).

Get your Google Analytics Tracking ID

From within Google Analytics, go to Admin -> Property -> Property Settings for the property you would like to log inbound calls for.  Copy the Tracking ID that is displayed and save it for later.

Add Custom Code to FreePBX

Copy the following code to /etc/asterisk/extensions_custom.conf.

exten => s,1,NoOp(******************************)
exten => s,n,NoOp(Google Analytics tracking for ${FROM_DID})
exten => s,n,NoOp(by: SMBTexas <>)
exten => s,n,NoOp(******************************)
exten => s,n,Set(GA_URL=
exten => s,n,Set(GTAG_ID=UA-xxxxxxxx-x)
exten => s,n,Set(GA_EC=Calls)
exten => s,n,Set(GA_EA=Inbound Call)
exten => s,n,Set(CURLOPT(conntimeout)=1)
exten => s,n,Set(CURLOPT(dnstimeout)=1)
exten => s,n,Set(CURLOPT(useragent)=Asterisk-FreePBX-GA-Call-Tracker-by-SMBTexas)
exten => s,n,Set(RESULT=${CURL(${GA_URL},v=1&tid=${GTAG_ID}&cid=${UNIQUEID}&t=event&ec=${GA_EC}&ea=${GA_EA}&el=${FROM_DID})})
exten => s,n,NoOp(Google Analytics result: ${RESULT})
exten => s,n,NoOp(Google Analytics tracking complete!)
exten => s,n,Return()

You want to update the GTAG_ID variable with your Tracking ID from above.  The GA_EC and GA_EA variables are text strings that are the Event Category and Event Action, respectively.  These defaults are OK for most usage but feel free to update them to fit your needs.

Create Custom Destination

Create a Custom Destination in the FreePBX GUI at Admin -> Custom Destinations.  Set the Target, Description, and Return fields as shown.  For the Destination, simply point to the next location the call should route to, i.e. a Time Condition, Call Flow Control, etc.  When done, click ‘Apply Changes’ in the top bar to make this an active destination for selection in your Inbound Routes.

Note, each Custom Destination has one specific destination to send callers to after doing the Google Analytics magic.  If your DID(s) have more than one destination they route to, you’ll need to create a Custom Destination for each route destination you need.  However, both the code in extensions.conf and the ‘Target’ of the Custom Destination will be as above, with only the destination and description for each Custom Destination being different.

Routing to the Custom Destination

In the FreePBX GUI, go to Connectivity -> Inbound Routes and edit your DID(s) to point to this new Custom Destination.  Click ‘Apply Changes’ to make the change active.

Back to Google Analytics

First, let’s make sure the event is logged via GA.  Go to Realtime -> Events in GA and then call the DID associated with the inbound route you just configured.  After a few seconds, you should see an event scroll across your screen!


Now, let’s set up a goal to capture this event.  In GA, go to Admin -> <your account> -> <your property> -> Goals.  Click the ‘New Goal’ button and configure it as such:

  1.  Goal Setup => Custom
    1. Press Continue
  2. Goal Description
    1. Name => Inbound call to xxx-xxx-xxxx
    2. Goal Slot ID => <next available>
    3. Type => Event
    4. Press Continue
  3. Goal Details
    1. Category <Equals to> => Calls
    2. Action <Equals to> => Inbound Call
    3. Label => <Your Inbound DID Number>
    4. Value => <leave blank>
    5. Use Event Value as Goal Value => No
      1. Set a value on this conversion.  Consult your SEO specialist(s) for this value.
    6. Press Save

Bonus Tip #1

Enterprising souls who have programmatic access to LRN lookups* through their trunk provider, Twilio, or other avenues COULD also get those details and populate that data as the value of this goal to GA.

*LRN lookups provide details about a phone number, such as the carrier that services that number, some geographic details related to the NPA-NXX, and if the number is listed as a wireless (cell) number or not.  Would capturing this level of information be helpful to a company?  Comment below!

Bonus Tip #2

This method of sending data to GA could be used in ANY voice platform that has the ability to make cURL or HTTP POST requests.  If you institute this in another platform, comment below and let everyone know.  Here is the documentation to reference!

That’s it!  Hopefully tracking inbound calls in google analytics is a useful integration for you and you enjoy it!

Contact Us