Untitled

mail@pastecode.io avatar
unknown
javascript
25 days ago
1.3 kB
4
Indexable
Never
// Get the URL
const url = window.location.href;

// Function to extract query parameters from the URL
function getQueryParams(url) {
  const params = {};
  const urlParts = url.split('?');
  if (urlParts.length > 1) {
    const queryString = urlParts[1];
    const paramPairs = queryString.split('&');
    paramPairs.forEach(pair => {
      const [key, value] = pair.split('=');
      params[key] = decodeURIComponent(value);
    });
  }
  return params;
}

// Extract the query parameters
const queryParams = getQueryParams(url);

// Extract the values of utm_source, utm_medium, utm_campaign, utm_channel, and utm_adname
const { utm_source, utm_medium, utm_campaign, utm_channel, utm_adname } = queryParams;

// Create a JSON object with the extracted values
const data = {
  utm_source,
  utm_medium,
  utm_campaign,
  utm_channel,
  utm_adname
};

// Serialize the JSON object
const jsonData = JSON.stringify(data);

// Send the serialized JSON to the server
// Replace 'your-server-url' with the actual URL of your server
fetch('your-server-url', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: jsonData
})
  .then(response => {
    // Handle the response from the server
    // ...
  })
  .catch(error => {
    // Handle any errors that occur during the request
    // ...
  });
Leave a Comment