Browsing Category

Tracking & Analytics

Google Analytics Tracking & Analytics

How to setup a User ID UID property in Google Analytics

Attracting all type of web traffic is becoming more expensive every day (that includes organic, paid and social), maximizing the value of every visit is key for any business to maintain a high profitability (same marketing budget with better ROI will significantly impact the bottom line). Performance marketing is becoming a popular term, as marketers we hear every day about metrics like:

  • Cost per click
  • Cost per lead
  • Cost per acquisition
  • Revenue per channel for ecommerce
  • Revenue per channel for off-line sales

Performance marketing is not possible without enough tracking metrics like the ones above, Google Analytics without any customization is able to provide some of those metrics like CPC and cost per lead but it fails to report on cost per acquisition and revenue per channel for off-line sales. To track those metrics and close the marketing loop we need to communicate data back from the CRM to Google Analytics. Using GCLID, CID and UID + the measurement protocol is the way to accomplish that.

Closing the marketing loop in Google Analytics:

Few years ago I published a post explaining how to communicate back from SalesForce to GA when a sale is close using CID, today I want to explain how to implement a CRM integration using UID, the main advantages of using UID over CID is that:

  • UID can track users through multiple devices while CID can not do that,
  • UID does not expire like CID, it is permanent

On the other hand there is a big problem with UID is that it needs users to be registered with your website (username and password), and use your website while they are logging in to their accounts (works well with shopping carts with recurring customers). In this post I will explain how to close the marketing look in GA using UID setup.

Step 1 - Create a GA UID view:

Create a new GA view with UID enabled (you can keep your old views but you need to start a new one using UID that has no data), in order to do that in your GA account: Click Admin >> navigate to the property in which you want to implement User-ID, in the PROPERTY column, click Tracking Info >> User-ID. Read more here how to complete the process.

Step 2 - Push UID value as a datalayer variable

In my example I will assume that you are using Google Tag Manger, once a user successfully logs in to their account the two datalayer events below must be pushed in the source code, the code below should be placed right after GTM code:

          event: 'login_verified',
          userId: crm_unique_id,

It is very important to have the userId value as a number that is saved in your CRM so you can use the same value to push data back to Google Analytics using the Measurement Protocol.

The code above must be pushed to every page a user visits after logging in, not only the first page they visits after logging in.

Step 3 - Capture UID in GTM

A new datalayer variable must be created in GTM:

The userId variable needs to be added to Google Analytics Settings variable in GTM as a custom field:


By completing this you should start seeing some data in the UID view you created in GA.

Step 4 - Pushing data from the CRM to GA

When a new sale happens in the CRM, the Measurement Protocol must be used to push the data back from the CRM to GA, we need to build dynamic URL and use POST method to push the data to GA (the URL below will push a sale to GA):

Check the table below to understand the values that were used in the example above, use the hit builder to test your calls.

Step 5 - Verify the data flow in GA

Go to audience then user explorer, find a userid that you pushed some data back to using the measurement protocol and make sure you can see the data there (something like below):



What happens to users that do not login, can I see them in the UID view in GA?

No, you will not be able to see those users at all, you will need to use the CID view.

What happens to a session where a user visits few pages without logging in then they login after?

Google Analytics will connect the dots for you and append the previous pages to the session when the user logs in. There could be some delays for that to happen and become visible in GA (possibly 24 hours sometimes).

Tracking & Analytics

How To Measure The Impact Of a TV Campaign on Web Traffic

There are a lot of trackable footprints that could be included in a TV ad, some of them are almost automatically included in the ad (like the brand name) others need to be added:

  1. The brand name, most TV ads will mention the brand name in a sound or tex format.
  2. A trackable dedicated URL or a domain. If your domain is you can include in the ad using a URL like or use a different domain like
  3. A trackable phone number that is dedicated only for the TV ad.
  4. A unique product name,  brand name + a product name or any statement that can trigger web search (example: cranberry sprite )

The more trackable footprints you have in the TV ad the easier it is for you to track the performance of this ad.

Metrics and tools that can be used to track a TV campaign:

  1. An Increase in search for the brand name  (trackable using Google Search Console).
  2. A significant Increase in search for the product name “i.e. cranberry sprite”  (trackable using GSC).
  3. A CTR improvement. The TV ad will keep the brand at the top of people’s mind so they tend to click more on any page produced by that brand even if it is a blog (trackable using GSC).
  4. An improvement in conversion rate for all products as a result of the brand enhancement (trackable in Google Analytics).
  5. A very significant improvement in sales and conversion rate for the promoted product in the TV campaign (i.e. cranberry sprite).
  6. Increase in brand’s equity (difficult to measure)
  7. More social buzz and inbound links as a result of people talking about the promoted product online (could be measured by tracking social signals and inbound links).

It is very important to discuss trackable footprints in the TV ad with the marketing team to come up with the best tracking options without jeopardizing the marketing part of the ad, after that a custom dashboard could be built using Google Data Studio could be built for that.

Google Tag Manager Tracking & Analytics

Using GTM to Track Shopify Conversions with Facebook and Bing

Shopify has a very nice integration with Google Analytics e-commerce tracking, all what it takes to enable it, is adding the property ID to Shopify and checking the enhanced e-commerce box, then going to Google Analytics to enable e-commerce tracking.

Integrating other codes like Bing or Facebook conversion code is not as easy, you can rely on the thank you page to trigger the pixels for those platforms but that will not be enough to provide very valuable data like the value of the transaction. Thankfully Shopify pushes all transaction variables to the source code of the thank you page using a JavaScript variable Shopify.checkout that has a JSON format:


That means you can add any value of that transaction as a JavaScript variable in Google Tag Manager, something like below:


You can read more about the available checkout variables here

Once you have this variable available in GTM you can use it in the sales tracking trigger (when the value is > 0 + thank you page):


You can also use its value in any pixel tracking code like Bing:

Tracking & Analytics

Integrating Google Ads Lead Form With CRMs

Google has announced the rollout of Lead Form Extensions that enable advertisers to capture form submissions directly from the ad without sending users to a landing page, the submissions will be stored in Google Ads' database and will be downloadable as a CSV file, there is also an option to integrate with CRMs using a webhook, this howGoogle explained the integration:

A webhook is an API that enables you to send lead data to your CRM system in real-time. To set it up, you will need to add a webhook URL and key to your lead form extension. You may need to do some configuration within your CRM system to generate the URL and key.

The URL is the delivery path: after the user submits a lead form, an HTTP POST request is sent to the configured URL, allowing the lead data to go directly into the CRM system. The key is used for validating the leads sent.

The explanation above is confusing for marketers and at least unclear for developers. What is the webhook URL and where to find it? This program (Google ads lead form extension) is in Beta now, and I am not sure if there is any CRM that supports it at the moment, even Salesforce doesn't have a webhook URL for it yet.

How to generate a webhook for the lead form extensions?

The best resource that can help with that is the developer guide provided by Google, anytime the form is submitted to Google a JSON POST request will be sent to the webhook URL added in the form lead extensions, something like below:

"lead_id": "lead_id1",
"form_id" : "form_id1",
"user_column_data":[ {"column_name":"Full Name","string_value":"John Doe"},
{"column_name":"User Phone", "string_value":"12345678"},
{"column_name":"User Email", "string_value":"[email protected]"}],
"google_key" : "secret"

Firs you need to decide what is the best way to capture this data, for me I am going to use PHP to do that, my webhook could be something like this  with the code below:

$json = file_get_contents('php://input');
$form_data = json_decode($json);
$leadid = $form_data->lead_id ;
$form_id = $form_data->form_id ;
$fullname = $form_data->user_column_data[0]->string_value;
$phone = $form_data->user_column_data[1]->string_value;
$email = $form_data->user_column_data[2]->string_value;

At this point all the values are available in the code and ready to be pushed to any CRM or a local database, most popular CRMs like SalesForce and Hubspot have APIs with PHP libraries (they have also libraries available for the most popular programming languages) making it easy to push the data to those CRMs.

Additional values like campaign and keyword could be available also in the fields and worth saving. Before pushing any values to your CRM make sure to create fields to match all the values you will be saving.


SEO Tracking & Analytics

How to Diagnose Organic Traffic Loss

Traffic loss is one of the most popular issues that can face SEO specialists, traffic can not go up forever and each website at some point will face a traffic loss situation that needs to be diagnosed. To help you to diagnose a traffic loss situation I will take you first thorough the most popular reasons to lose organic traffic, understanding those reasons and learning how to monitor them will make diagnosing traffic loss an easier task.

Most popular reasons to lose traffic:

1- Ranking loss:

Position #1 in the SERP can enjoy 30% or more click through rate, after that CTR will go down for every lower position in the SERP, position #10 can get 5% CTR or less. If a website losses ranking for highly searched keywords, the overall organic traffic will go down.

The most popular reasons for ranking loss are:

  • Algorithm updates (search engines like Google runs multiple updates every year).
  • Losing authority (e.g. Losing a lot of quality inbound links) or slow link growth.
  • Losing popularity (e.g. Less social signals and lower branded searches).
  • Website stagnation (no new content or no content refreshment).
  • CMS change or content change (e.g. website redesign which can include CMS change, URL change and content change).
  • Increase in competition, competitors could be providing better content and promoting their website more, so they get higher ranking.
  • Technical issues with search engines like crawlability, indexability, downtime, slow loading and manual actions.

2- SERP layout Change:

Google keeps changing the SERP layout, putting more ads at the top sometimes or featured snippets which can affect CTR while keeping the same ranking.


In the example above even a website ranks #1 it will be still below the fold, which can bring CTR significantly down, so we are not going to enjoy 30% CTR being number one anymore, we could be receiving 10% CTR only with the new layout.

3- Trending change and user behaviour:

Human needs and behaviours change throughout the years. Products and brands get disrupted sometimes and that can change search volume. A product like mini DVD has been disrupted by smart phones and tablets, what brought the search volume for it close to zero:


Another example could be online dating, the need for dating did not decline nor the need for online dating, but social media websites like Facebook are becoming a go-to destination for people that are looking for dating, which brought down the interest for the keyword online dating:


Step by step traffic loss audit:

Now that you know the most popular reasons to lose traffic, it is time to run some analysis on key metrics that can help us to evaluate a website against each traffic loss reason.

1- Identify which keywords are losing traffic using GSC:

Any traffic loss will be linked eventually to ranking and search volume for the keywords that are driving organic traffic, when a website is losing organic traffic the direct reason for that is either some keywords are getting less traffic or less keywords getting traffic, so the main focus of any traffic loss audit should be identifying which keywords are losing traffic, thankfully this analysis is made easier with GSC, just use the comparison feature and choose the too time spans that you want to analyse for traffic loss and identify which keywords are causing that loss:


Once you identify the keywords you need to assess what is causing them to lose traffic by running them against the reasons of losing ranking explained above.

2- Check the brand name in Google trends to make sure that the brand is not losing any popularity.

Run the checking for the last 5 years in the target audience country.

3- Link profile analysis:

Check the domain using Majestic SEO or Ahrefs to see if there is any recent link loss, the screen shot below is taken from Majestic:


Link loss will lead to lower authority which in most cases will cause ranking loss.

4- Check index growth in Google Search Console:

This will help to identify any deindexation issue:


Losing more indexed pages means losing ranking for any keywords those pages are ranking for. Deindexation could be a result of:

  • Technical errors in the website (e.g. server errors or very slow load time).
  • Duplicate or thin content.
  • Issues with setting up the canonical tags.
  • Algorithm update that affect crawling standards.
  • Losing authority (e.g. the link profile is getting weaker).


Organic traffic loss can happen to any website, diagnosing the situation and finding the reasons behind it is not quick nor easy in most cases, sometimes you get lucky to find out that a technical issue on the site caused it, the webmaster can fix it quickly and things will go back to normal in few weeks, but for most other cases recovering may not be even possible or can take a very long time. The key thing when it comes to traffic loss is finding about early and start reacting to it immediately.



Tracking & Analytics

How To Build Your Own Call Tracking System Using Twilio

This post is an extension to the previous post I did explaining how to connect Twilio to Google Sheets and Google Data Studio , the missing part in that post was how to designate different phone numbers to different mediums or sources of a website traffic, and this is what I will be covering in this post.

Step 1:

Decide what mediums or sources you want to track, the most basic tracking if you have a low budget is tracking three mediums: organic, paid and others, a more advanced tracking will be:

  • Organic Google
  • Organic Bing
  • Paid Google
  • Paid Bing
  • Referral
  • Direct

You can also consider tracking email campaigns and offline campaigns like radio. Obviously the more mediums/sources you want to track the more phone numbers you need.

Step 2:

Assuming you are going after a basic tracking (organic, paid and others) you need to buy three phone numbers, I will assume they are:

  • 111-111-1111 for organic traffic
  • 222-222-2222 for paid traffic
  • 333-333-3333 for others

Make sure to redirect those numbers to your own phone number, I will call that the default phone number and it is 000-000-0000

Step 3:

We need to save the source, medium and term for every website visitor in a cookie, you can do that using UTMZ-replicator  just add it to your website using Google Tag Manger as a custom HTML tag and let it fire on every pageview.


Step 4:

Make sure the phone number on your website is contained in a unique class, something like below:

<a class="default-phone-number" href="tel:000-000-0000">000-000-0000</a>

Step 5:

Add the JavaScript code below as a tag in Google Tag Manager and let if fire after the UTMZ tag in step 3:

var findnum = [
{ "number": "111-111-1111", "medium": "organic" },
{ "number": "222-222-2222", "medium": "cpc" },
{ "number": "333-333-3333", "medium": "others" }
function searchMD(findnum, medium){
for(var i= 0, L= findnum.length; i<L; i++){ if(findnum[i].medium=== medium) return findnum[i].number; } return ''; } var ga_source = ''; var ga_campaign = ''; var ga_medium = ''; var ga_term = ''; var ga_content = ''; var gc = ''; var c_name = "__utmzz"; if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
if (c_end==-1) c_end=document.cookie.length;
gc = unescape(document.cookie.substring(c_start,c_end));
if(gc != ""){
var y = gc.split('|');
for(i=0; i<y.length; i++){ if(y[i].indexOf('utmcsr=') >= 0) ga_source = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmccn=') >= 0) ga_campaign = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcmd=') >= 0) ga_medium = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmctr=') >= 0) ga_term = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcct=') >= 0) ga_content = y[i].substring(y[i].indexOf('=')+1);
if (!((ga_medium =="organic") || (ga_medium =="cpc"))) ga_medium = "others" ;
document.getElementsByClassName('default-phone-number')[0].innerHTML= searchMD (findnum,ga_medium);
document.getElementsByClassName('default-phone-number')[0].href= "tel:"+searchMD (findnum,ga_medium);


At this point you can be sure that phone number will fire only based on their correspondent medium, at this point you can start building your dashboard following the post here

Tracking & Analytics

Connect Twilio to Google Sheets and Google Data Studio

Call tracking is a vital service for any marketer, and when it comes to call tracking, Twilio is the largest telephony infra structure provider in the market that can help a lot with that; however, Twilio is geared more towards developers, nontechnical users can not do much using Twilio and they have to go for a plug and pay services like, Dialogue Tech and Call rail.

In this post I will provide Twilio's nontechnical users with a solution to create a friendly dashboard using Google Sheets, which could be eventually used with Google Data Studio for better visualization.

Step 1:

Make sure you have a Twilio account with at least one active phone number that redirects to your own phone number, Twilio has a nice guide here that explains how to find and buy a phone number, after buying the phone number you can use Twilio's built in web-hook to redirect it to your own phone number (see below)


Just add your phone number to the end of the web-hook's URL:

You can buy multiple phone numbers and use each of them to track a different medium, like organic, paid, and others. In this tutorial  you can find how to rotate phone numbers based on incoming traffic to your website, if you are using the the numbers on dedicated landing pages or other mediums like off-line, number's rotation will not be required as each number will be served only for one medium.

Step 2:

Find your Twilio's API credentials by clicking the gear icon at the top >> settings >> copy ACCOUNT SID and AUTH TOKEN


Step 3:

Replace your ACCOUNT SID and AUTH TOKEN in the code below that we will be using in Google Sheets:

function myFunction() {
var ACCOUNT_SID = "*********************************";
var ACCOUNT_TOKEN = "*********************************";
var findmedium = [
{ "number": "+1**********", "medium": "organic" },
{ "number": "+1**********", "medium": "cpc" },
{ "number": "+1**********", "medium": "others" }
// you do not need to edit anything below this line
function searchMD(findmedium, num){
for(var i= 0, L= findmedium.length; i<L; i++){
if(findmedium[i].number=== num) return findmedium[i].medium;
return '';
var numberToRetrieve = 10;
var hoursOffset = 0;
var options = {
"method" : "get"
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(ACCOUNT_SID + ":" + ACCOUNT_TOKEN)

var url="" + ACCOUNT_SID + "/Calls.json?PageSize=" + numberToRetrieve;
var response = UrlFetchApp.fetch(url,options);

var theSheet = SpreadsheetApp.getActiveSheet();

function search(xyz) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
for(var i=2;i<12;i++){
var value = sheet.getRange(i, 6).getValue();
if(value == xyz){
var match = 1 ;
return match ;
var startColumn = 1;
var theRow = 2 ;
var dataAll = JSON.parse(response.getContentText());
for (i = dataAll.calls.length -1 ; i >= 0 ; i--) {
var found = search (dataAll.calls[i].sid) ;
var calldirection = dataAll.calls[i].direction ;
var midnight = new Date();
midnight.setHours(0, 0, 0, 0);
rowDate = dataAll.calls[i].date_created;
var theDate = new Date (rowDate);
if (!found && calldirection == "inbound") {

theColumn = startColumn;

if(isNaN(theDate.valueOf())) {
theDate = 'Not a valid date-time';
else {
theSheet.getRange(theRow, theColumn).setValue(theDate);

var callduration = dataAll.calls[i].duration/60 ;
callduration = +callduration.toFixed(2);

theSheet.getRange(theRow, theColumn).setValue(dataAll.calls[i].to);
theSheet.getRange(theRow, theColumn).setValue(dataAll.calls[i].from);
theSheet.getRange(theRow, theColumn).setValue(callduration);
theSheet.getRange(theRow, theColumn).setValue(searchMD(findmedium, dataAll.calls[i].to)); //tw searchMD(findmedium, dataAll.calls[i].to) ;
theSheet.getRange(theRow, theColumn).setValue(dataAll.calls[i].sid);

If you are using multiple phone numbers for different mediums replace the values in the findmedium array, otherwise just leave the code as is.

Step 4:

Create a new sheet in Google Sheets and name the first 6 columns in the top row as below (feel free to change the names to any other names keeping the same order)


Step 5:

In Google Sheet go tools >> script editor >> delete any existing codes and replace them with the codes from step 3


Step 6:

In Google Sheets script editor click on the clock icon to create a trigger >> Click + Add  Trigger (bottom right corner) >> Time-driven >> Hour timer >> Every hour >> save


Once you do that, in few hours the data will start to show up in your sheet as below:


Having the call date in Excel will give you a lot of flexibility to manipulate and extract data, for more visualization read step 7 which will explain how to create a simple report in Google Data Studio.

Step 7:

Connect the sheet you created to Google Data Studio by going blank report >> Create data source >> Create new data source >> Google Sheets >> Choose the spread sheet created in step 4 >> Click connect


You should be able to see an empty Google Data Studio report with a grid, now Insert >> Time series >> Add to report

If everything works fine you should be able to see a report like below (a daily total calls duration report) :


There is a lot you can do to customize this report but I am not going to go through all of that, watch this video here to learn more.

Finally I want to say that this post was inspired by a great post provided by Twilio How to Receive SMS Messages into Google Sheets with Apps Script and Twilio and part of code was used in my post.

Please feel free to comment if you have any questions.

Tracking & Analytics

How to Pass Google Analytics Traffic Source Dimensions To Salesforce

Would not be great if marketers can see Google Analytics traffic source dimensions in Salesforce? Unfortunately Salesforce by default doesn't provide any attributions for web leads pushed to Salesforce, I am not sure why considering that is not difficult to do that for a lead that is pushed using web-to-lead form where a simple additional code can push the lead source dimensions like source/medium and more other dimensions that marketers badly need to calculate ROI. This guide will provide a step-by-step method explaining how to push the lead source dimensions mainly  source, medium and keyword (if available) to Salesforce, I will be using the same terminology used by Google Analytics to keep things consistent for marketers.

Step 1:

We need first to create few extra fields in Salesforce to save the lead source dimensions, in sales force go Salesforce > Set up >  Object Manager > Lead > Fields & Relationships > New > Text


We need to repeat the process above few times depending on the number of diminutions we are willing to push to Salesforcee, in my case I have created three dimensions Medium, Source and Term (keyword).


It is recommended to choose a long text area field for the Source as some URLs can get really long .

Step 2:

Now we have the fields ready in Salesforce, we need to create a Web-to-Lead form which you can access through Salesforce > Setup > Web-to-lead > Create Web-to-Lead Form   (make sure to add Source, Medium and Term to the form).


Generate the form then hide the Source, Medium and Term fields, you need also to clean up the extra text related to those 3 fields.


Step 3:

We need to save the Source, Medium and Term for every website visitor in a cookie, you can do that using UTMZ-replicator  just add it to your website using Google Tag Manger as a custom HTML tag and let it fire on every pageview.


Once this script is added you will be able to see Source, Medium and Term saved in a cookie called __utmzz


Step 4:

Source, Medium and Term are saved in a cookie now it is time to take their values and populate them to the corresponding hidden fields in Salesforce's web-to- lead form, to do that you just need to create a tag in Google Tag Manger (custom HTML tag) and insert the code below (the cookie reading part of this code is taken from Stackoverflow with some modifications)

<script type="text/javascript">
var ga_source = '';
var ga_campaign = '';
var ga_medium = '';
var ga_term = '';
var ga_content = '';
var gc = '';
var c_name = "__utmzz";
if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
if (c_end==-1) c_end=document.cookie.length;
gc = unescape(document.cookie.substring(c_start,c_end));
if(gc != ""){
var y = gc.split('|');
for(i=0; i<y.length; i++){
if(y[i].indexOf('utmcsr=') >= 0) ga_source = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmccn=') >= 0) ga_campaign = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcmd=') >= 0) ga_medium = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmctr=') >= 0) ga_term = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcct=') >= 0) ga_content = y[i].substring(y[i].indexOf('=')+1);
document.getElementById('00N3i00000BG1jK').value= ga_source;
document.getElementById('00N3i00000BG1hp').value= ga_medium;
document.getElementById('00N3i00000BG1lf').value= ga_term;

You can see that the code above includes also campaign which I did add to Salesforce but you can easily add it following the process provided early in this guide. After publishing the code above and submitting a form lead you should be able to see the lead source dimensions (Source, Medium and Keyword) in Salesforce lead page.


Please feel free to comment below if you have any questions

Tracking & Analytics

Salesforce Integration With Google Analytics - Step by Step

Salesforce is probably the most popular cloud based CRM in the market but unfortunately it is lacking the ability to integrate well with web tracking systems like Google analytics (at least the free version) to provide a full view of the user journey from A-Z (web lead to sale).

On the other hand Google Analytics (the most popular website traffic software) gets very close to provide the full user journey but as Google Analytics is not a CRM it is missing conversion tracking data (mainly leads tracking), a lead could be tracked easily in GA by tracking the lead  form thank you page but once this lead is pushed to a third party CRM like Salesforce GA can not get any information when and if that lead is closed. Google Analytics 360 has the ability to integrate with Saelsforce and closing the loop on conversion tracking but unfortunately GA 360 cost is around $150,000/year

In this step by step tutorial I will show you have you can integrate GA (the free version) with Salesforce and get the conversion tracking data, before start doing that I want you to understand my working environment and to be aware that it may not work 100% for you but with some modifications you can get this solution to work with any environment, :

  • The CMS I am testing this work on is WordPress, it is PHP based so all my codes will be in PHP
  • I am using Google Tag Manager to implement some JavaScript codes
  • I am using Salesforce' PHP simple implementation to interact with the Salesforce REST API from PHP
  • I am using the 30 days free Salesforce trial

Stage 1 - push Google Analytics CID to Salesforce:

Client ID (CID) is a unique user identifier created by Google Analytics and stored in a browser's cookie for two years, the number could be seen in GA's user explorer and also could be found in the browsers cookie


All subsequent activities by the same user using the same browser will be listed under that CID and treated as one user's activities


Step 1

Before even trying to push CID to Salesforce we need to create a new lead field in Salesforce by going to Salesforce > Set up >  Object Manager > Lead > Fields & Relationships > New


Keep the field name CID__c handy as we will need it when we communicate with Salesforce using the API

Step 2:

Now we have the CID field ready in Salesforce, filling that field could be done in different ways, in my case I will be using the Web-to-Lead form which you can access through Salesforce > Setup > Web-to-lead > Create Web-to-Lead Form  and generate a source code that you can use on your website


The form will be used in the contact us or lead generation page, before placing the form make sure you make the CID field hidden from users as it will be filled automatically using JavaScript. See an example of the Webto-Lead form with the CID field hidden:


Step 3:

Now we need to inject the CID value inside the hidden field, this could be done by reading the GA cookie via JavaScript (not recommended by Google) or by using native Google Analytics calls. I will be using native Google Analytics calls and include them using Google Tag Manger, the tag below will be able to find the CID and inject it in the hidden CID field (make sure to set up the right firing sequence as below)


By now the form is fully communicating with Salesforce and passing the CID, you can make a test and you will see the CID showing in Salesfoce's dashboard


Stage 2 - push a pageview back Google Analytics from Salesforce with the same CID once the lead is closed:

For this stage we need to communicate with Salesforce using their API, we need to build a system that can check the leads status every 5-10 minutes and if any new lead is becoming closed we need to push a pageview back to Google analytics using the measurement protocol

Step 4:

Create an APP in Salesforce to enable the API connection Salesforce > Set up >  > Apps > App Manager > New Connected APP >> Save >> Continue


Save the consumer key and consumer secret from the next screen to use them in the API SDK


Edit the App permissions Salesforce > Set up >  > Apps > App Manager > Manage Connected APP >> Edit >> Save


You do not have to use exactly those settings, I just tried to keep the policies very lose to make building my first App easier

Step 5:

Download the developer SDK for your preferred programming language, in my case I will be coding in PHP so I can use the PHP developer SDK for Salesforce or  the simple implementation example here which I ended up using just to keep things simple for this tutorial. When using the simple implementation example here (or any other framework ) make sure to rename the call back file to match the call back URL in step 4 and edit the config.php file and add the credentials you got from step 4:


Visit the index.html file which will redirect you to login to your Salesforce account and after that you will be brought back automatically to the file demo_rest.php now you will have 15 minutes to test anything you want, once you are happy with the results you need to use the refresh token which you can get in the callback file using this code:

$refresh_token = $response['refresh_token'];

Step 6:

After establishing a successful connection using the API you need to write a call that list all or part of the leads:


The output of the file will be as below:


As you can see we have now a list of the leads with their CID and their status, what we will be processing in the next step are leads that contain CID and the are Closed - Converted

Step 7:

Create a new goal in Google Analytics with a destination URL like this /thank-you-page.html  or any other URL of your choice that is not used by any other goal, create also a page with no content for that URL.

Use Google Analytics Hit Builder to build a call that add a pageview for /thank-you-page.html


You can click send a hit to Google analytics to test the URL but eventually this URL must be part of your script (a cronjob) that will:

  • Scan Salesforce's leads using the API every few minutes or every hour
  • Find closed leads
  • Push a pageview hit to Google Analytics using the lead CID
  • Store that lead in an internal database that will be checked in the future to avoid saving duplicate leads

If the lead with right CID is pushed correctly to Google Analytics as a pageview, you should be able to see that pageview and a registered goal using the users' explorer


You can see how the medium organic search was inherited automatically using the existing CID without including that in the hit builder URL, at this point we have closed the loop on finding actual conversions that happen in Salesforce but missing in GA, the user flow could be checked in GA uninterrupted without any additional actions by GA users.


Good luck implementing that solution, please comment below if you a have a question



Tracking & Analytics

Tracking and Analytics

"If you can not measure it, you can not improve it" results measurement is vital in any marketing campaign, luckily when it comes to online marketing there is almost nothing that could not be measured. There are many metrics that SEOs need to keep eye on for different reasons:

  • They are ranking factors (e.g. website speed)
  • They are success signals (e.g. more organic traffic)
  • They are alarming signals that need to be investigated (e.g. ranking with Google is declining or GSC sending error notifications)
  • They present opportunities (e.g. keywords that rank on the second page can easily make their way to the second page with some optimization)

There are a lot of SEO KPIs to track, so before starting any project you need to sit with the project's stakeholders and discuss what KPIs need to be tracked, have not said that there are many standard (universal) SEO KPIs that most SEOs track that I will focus on in the next section.

Organic traffic tracking metrics:

The metrics below could be tracked using Google Analytics (GA), Google Search Console (GSC) and Google My Business. Please note that GSC and GMB do not save a long history of data so you need to make sure you have a solution that keep the data stored permanently

  • Number of organic landing pages (GA)
  • Head (top) keywords ranking (GSC or third party tools)
  • Average position/ranking for all keywords (GSC)
  • Number of indexed pages (GSC)
  • Pages with errors (GSC and GA)
  • CTR (click through rate) (GSC)
  • Bounce rate (GA)
  • Time on site (GA)
  • Pages per session (GA)
  • Number of keywords ranking on the first page (GSC with some processing)
  • Pages that attract zero traffic (GSC + site crawling data)
  • Pages and keywords that are losing traffic  (GSC)
  • Google My Business GMB, impressions, directions, phone calls and clicks (GMB)

It is very important to understand the average values of the metrics above for your industry and set objectives/forecasts for every new project with a clear plan to achieve those forecasts

Website performance, UX,  CRO and ROI

There are a lot of metrics to create and track that can help with UX and CRO

For UX many of them are mentioned above:

  • Bounce rate
  • Time on site
  • Number of pages per session
  • Event tracking (e.g. video play duration and links clicking)

For CRO:

  • Goal tracking (e.g. tracking form submissions)
  • Ecommerce tracking for websites that sell online products
  • Connecting CRMs to Google analytics to track sales (e.g. connect SalesForce to Google Analytics)
  • Audiences building and tracking
  • Phone calls tracking by medium/source
  • Clickable phone number event tracking (this is for mobile users that click to call on a phone number)

It is very important to report on the financial success of the SEO campaign, one way to do that is ROI (return on investment) which is simply  ((Monthly Earnings - Monthly SEO retainer)/Monthly SEO retainer) X 100 some people go with revenue instead of earning. While doing this you need to consider the customer life time value, for a client that offers web hosting  where customers rent servers and keep them almost for life the monthly earning or revenue will not reflect the actual value of this customer, if they are selling $200/month dedicated servers with 30% profit then the earning of one sale should be 0.3 X 200 X (customer average lifespan let us say 36 months) = $2160 .. so if you are paid $1080/month for your SEO services and you are able to generate one sale for this client the ROI of the SEO services will be 100% in this case

Another way to justify ROI if you do not have access to the client financial data is going by the click value, if you have a personal injury lawyer client where a click using Google Search Ads can cost a $100 or more for some keywords, being able to drive 50 clicks/month from relevant keywords can justify an SEO spend of $2000 or more

Competition analysis:

Picking the right tools to do competition analysis is key to come up with any meaningful insights, in order to keep it apple to apple you can not use GSC to measure your own traffic then use SEMrush to measure competitors traffic, just use the same tool for everyone.

We are very lucky to live in an era where tools like SEMrush and Ahrefs are available to provide very useful link graph and traffic data for almost any website on the web, the key metrics to track while doing competition analysis are:

  • Referring domains
  • Inbound links
  • Organic traffic
  • Ranking for highly searched keywords
  • Number of keywords on the first page
  • Number of indexed pages

All of them are available using SEMrush with an exception of number of indexed pages which you can use Google modifier to track them

The most popular tools used by SEOs for tracking key metrics:

  • GMB (Google My Business)
  • GSC (Google Search Console)
  • GA (Google Analytics)
  • GTM (Google Tag Manager)
  • SEMrush
  • Ahrefs
  • Google Data Studio

Offering tracking services:

With tools like GA and GTM tracking is becoming a service on it own that SEOs can provide to their clients, here are few areas where clients need help:

  • GA optimization, Google recommends creating multiple views in each property, each property needs it is own filters and goals
  • Mapping events and goals: After a long time using the same view and many parties adding their own filters, goals, audiences and customization there will be a need for someone to come and map all of this and do some clean up
  • Many clients end up with 100s of tags and triggers in their GTM where it becomes very difficult to navigate and understand so it needs some mapping and clean up
  • Self submission forms and complex events tracking need GTM and GA experts to implement them
  • Ecommerce tracking is normally difficult for many webmasters to implement on their own unless it is natively supported by the shopping cart software (e.g. Shopify)
  • Creating GA and GDS custom dashboards

Gaining advanced knowledge in GA, GSC and GTM is vital for your SEO career, make sure you check the available certificates here that can help you to gain and validate the tracking knowledge you need to support your SEO career