4

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>
Siva
  • 51
  • 1
  • 4
  • have you tried wrapping the analytic script using https://amp.dev/documentation/components/amp-mustache/ ? – Jose CC Nov 27 '19 at 02:29
  • if you backend preloads Json this can be very easy using amp-mustache but is not what you may be able to do is using amp-script and then pass the variable to the analytic script – Jose CC Nov 27 '19 at 02:32
  • Have you tried what @JoseCC suggested.? Were you able to pass calculated values from amp-script to amp-analytics vars. – freesoul Feb 21 '20 at 08:09
  • @Siva I don't think you need to add anything custom in your template. What you can do is add a custom script in tag manager to pull that data – Jose CC Feb 25 '20 at 21:58

1 Answers1

0

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>
Matt Welke
  • 1,441
  • 1
  • 15
  • 40