How to pass dynamic values to <amp-analytics> var

876 Views Asked by At

I need to pass the pagePath value dynamically into my amp-analytics tag's var value. Can you please advise how i can achieve this. For example, In the below code snippet , I need to replace the pagePath with different values based on the current page path.

<amp-analytics config="https://www.googletagmanager.com/amp.json?id=xxxxxxxxx&gtm.url=SOURCE_URL" data-credentials="include"
    <script type="application/json">
          {
              "vars" : {    
        "pagePath" : { Needs to be passed dynamically }
              }
          }
    </script>
<amp-analytics>

1

There are 1 best solutions below

0
Matt Welke On

You would use variable substitution (https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md).

The variable you're interested in is "Canonical Path" (https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md#canonical-path).

You would insert the string ${canonicalPath} wherever you want it to be templated in before the analytics request is sent. For example:

<amp-analytics config="https://www.googletagmanager.com/amp.json?id=xxxxxxxxx&gtm.url=SOURCE_URL" data-credentials="include"
    <script type="application/json">
          {
             "vars" : {       
              "pagePath" : "${canonicalPath}"
               }
          }
    </script>
<amp-analytics>