-1

I want to include a tracking script in my Drupal website and inside the script I want to include things like, title of page, description etc. In other words, I need to include the machine names inside the script.

But how can I do that. I need to change the %TEXT% to %MACHINE_NAME%. The script is here:

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Event",
"location": {
"@type": "Place",
"name": "%company-name%",
"logo": "%logo-url%",
"alternateName": "Universe Science Park",
"address": {
"@type": "PostalAddress",
"streetAddress": "%address%",
"postalCode": "%postalcode%",
"addressLocality": "%city%",
"addressRegion": "%region%",
"addressCountry": "%country%"
}
},
"name": "%event-title%",
"description": "%event-descriptions%",
"startDate": "%startdate-and-time%",
"endDate": "%enddate-and-time%",
"image": [
"https://example.com/photos/1x1/photo.jpg",
"https://example.com/photos/4x3/photo.jpg",
"https://example.com/photos/16x9/photo.jpg"
],
"inLanguage": "%language%"
}
</script>
  • You can use the Schema Metatag module () to output LD+ Json, and it will allow for using tokens to create the values you want. – Jaypan Mar 18 at 17:15
  • @Jaypan Found a solution. Read my comment below – BLS Mar 19 at 11:37
1

I found a solution for this. If somebody ever comes across same problem here is how you can manage it.

  1. Download the module: https://www.drupal.org/project/tracking_code
  2. After installing and activating go to Structure -> Tracking Code Snippets
  3. Add tracking code
  4. Here you can use tokens and add machine names

This is very simple and helped me alot!

I hope this will help you too. Cheers

0

You are going to want to put together a new custom module.

At first your only going to need a few files:

  • modules/custom/tracking/
    • js/
      • tracking.js
    • tracking.info.yml
    • tracking.libraries.yml

Define a new library for your javascript to live in, and add dependencies for core/jquery and core/drupalSettings. You can then create the file and follow the Javascript API to implement access to drupal.settings to get some of these items from Drupal directly.

Your other variables can be derived by JS or you can write your own PHP in the future if you need to send a new setting and expose it to the library. At this point your enabled to do whatever you would like to do.

Sources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.