V
V
Vince2019-02-07 19:51:29
WordPress
Vince, 2019-02-07 19:51:29

How to catch UTM tags in Wordpress and send everything to Google tables?

Greetings.
An unusual task came to me );
There is a landing page made on Wordpress. The customer claims that all links that people come to the site are assigned a utm label set by him.
Previously, the landing page was on tilda and everything was caught there and added to the Google plate, but now everything is on Wordpress.
So here are the tasks 2
The client wants to understand where the visitor came from, who eventually bought the ticket. In this case, the link to the purchase leads to another site.
And the second task is to send this data to Google spreadsheets.
Please give advice - how to implement this? Maybe there are ready-made solutions?
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2019-02-07
@pton

I don’t know about catching tags, but sending data to a google table is simple
. The principle is this: Collect data, make a POST request to google. This query catches your script and parses the data into table cells.
for this
In google spreadsheet go to "Tools->Script Editor" and write the reception code

function doPost(e) {
    
  if(typeof e !== 'undefined')
    var par = JSON.parse(e.postData.contents);

  PutDataInSheet(par);
  return ContentService.createTextOutput("ok");  
}


function PutDataInSheet(e) {
  // в кавычках ключ вашей таблицы, его взять в адресной строке, когда вы находитесь в вашей таблице
  var wbk = SpreadsheetApp.openById("1Q3yDiWwOMosybBho_attZYW6roT4SOP4-oHPDARzA_s");  // таблица - февраль 2018
 
  // далее идет разбор данных и раскладка по ячейкам
  var wks = wbk.getSheetByName("сайт");
  var row = wks.getLastRow()+1;
  var d = new Date(); 
  wks.getRange("A"+row).setValue(e['updated_at']);
  wks.getRange("B"+row).setValue(e['id']);
  wks.getRange("C"+row).setValue(e['billing_address']['last_name'] + ' ' + e['billing_address']['first_name']);
  // Убираем все символы кроме цифр из номера телефона
  var phn = e['billing_address']['phone'];
  phn = phn.replace(/[^\d]/g, "");  
  wks.getRange("D"+row).setValue(phn);
  wks.getRange("E"+row).setValue(e['billing_address']['address_1']);
  wks.getRange("G"+row).setValue(parseFloat(e['subtotal']));
  for (i in e['line_items']) {
    var item = e['line_items'][i];
    wks.getRange("J"+row).setValue(item['sku']);
    wks.getRange("K"+row).setValue(item['quantity']);
    wks.getRange("L"+row).setValue(parseFloat(item['subtotal']));
    wks.getRange('A' + row + ':L' + row).setBackground('#d9ead3');
    row ++;
  }
}

Save the project and select "Publish->Deploy as Web Application". Get a link to access the app. It must be copied and pasted into the server code
Next, you need to write the server code to send
/**
     * выполняет отправку заказа в google таблицы 
     */
    public static function SendOdrerToGoogleSheet($order_id){
        // собираем данные в массив
        $order = self::getOrderDetailById($order_id);

        
        // ссылка на WEB приложение
    $url = 'https://script.google.com/macros/s/AKfycby_0wdvKJuWbRZEdJUY1xeRWXmUiRTOuX584HhAxtde4y8ZKJ4/exec';
                    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);   // переходит по редиректам
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71 Safari/534.24");
        curl_setopt($ch, CURLOPT_POST, 1); // Do a regular HTTP POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order)); // Set POST data

        $response = curl_exec($ch);
        curl_close($ch);
    }

That's it. The code is taken from a working tool.
The only thing I haven't figured out is how to set up CURL for asynchronous transmission. Well, don't wait for an answer. We send the data unilaterally, why slow down the server by waiting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question