0

I am trying to embed Google Trends graphs on pages of a Wordpress site, using the embed code provided by Google Trends themselves, for instance:

<script type="text/javascript" src="https://ssl.gstatic.com/trends_nrtr/1709_RC01/embed_loader.js"></script> <script type="text/javascript"> trends.embed.renderExploreWidget("TIMESERIES", {"comparisonItem":[{"keyword":"sample word","geo":"","time":"2004-01-01 2019-02-24"}],"category":0,"property":""}, {"exploreQuery":"date=all&q=sample%20word","guestPath":"https://trends.google.com:443/trends/embed/"}); </script>

The problem is that, as mentioned in the embed modal:

This data is static and will not update

enter image description here

However, it looks like that problem could be solved by updating the end date defined in the embed code for the time parameter to the value of the current date, thus making sure the graph is always up to date:

"time":"2004-01-01 2019-02-24"

Where today's date would replace 2019-02-24 above.

Can this be done with a simple var date = today.getDate();? If yes, what is the cleanest way to include that in the embed code?

Thibaud Clement
  • 6,607
  • 10
  • 50
  • 103
  • 1
    Yes, replacing the date-time as you suggested should work. The – Guy Yogev Mar 08 '19 at 23:06
  • @GuyYogev actually, using `var date = today.getDate();` breaks the embed code and nothing is displayed. I am not sure yet whether this has to do with the fact that the embed code is not supposed to be tempered with, or with the fact that the date format of the `date` variable may not be `YYYY-MM-DD`. Still investigating, but if you have some thoughts on this, I am all ears. – Thibaud Clement Mar 24 '19 at 02:52
  • 1
    The embed code is a normal JS code. You can do whatever you wish. The dateshould be formatted correctly to YYYY-MM-DD Here is a [working example](https://jsbin.com/sawuyiruya/edit?html,output). – Guy Yogev Mar 25 '19 at 06:46
  • @GuyYogev thanks a lot (apologies for my very late response). Can you think of any reason why that would not work on a Wordpress blog? – Thibaud Clement Jun 05 '19 at 09:27
  • @GuyYogev never mind, I figured it out, everything works great. Thanks a lot. Feel free to submit an answer, I will be happy to accept it. – Thibaud Clement Jun 05 '19 at 11:18

0 Answers0