Commit 8e7ed5be authored by Shane Rice's avatar Shane Rice

added if statements to dataLayer elements

parent d6657ecf
Pipeline #45837298 failed with stages
in 17 minutes and 10 seconds
......@@ -2,9 +2,12 @@
// Creates a dataLayer above GTM tag
%script{ type: "text/javascript"}
dataLayer = [{
\ 'postType': '#{current_page.data.postType}',
\ 'campaign': '#{current_page.data.campaign}',
\ 'tags': #{current_page.data.tags.split(", ")}
-if current_page.data.postType
\ 'postType': '#{current_page.data.postType}',
-if current_page.data.campaign
\ 'campaign': '#{current_page.data.campaign}',
-if current_page.data.tags
\ 'tags': #{current_page.data.tags.split(", ")}
}];
%meta{name: "viewport", content: "width=device-width, initial-scale=1.0"}
......
  • @jareko whats the best method to find the logs for this if I build this locally? Failing on my machine too and I can't scroll back up to see output.

    Added these if statements to keep the dataLayer empty if we're not using it after I saw the pipeline fail Friday afternoon.

    Thanks!

  • @shanerice The if statements should live outside the dataLayer, if I'm not mistaken. So it would be:

    %script{ type: "text/javascript"}
      if (current_page.data.postType) {
        dataLayer = [{
          'postType': '#{current_page.data.postType}'
        }]
      }
      if (current_page.data.campaign) {
        dataLayer = [{
          'campaign': '#{current_page.data.campaign}'
        }]
      }
      if (current_page.data.tags) {
        dataLayer = [{
          'tags': #{current_page.data.tags.split(", ")}
        }]
      }

    But if two of these existed, dataLayer would fire twice. Does that matter? Or is it fine to have it the way it was, without the if statements? So, if only one exists, the others just show up as blank.

  • @jareko thanks for the suggestion! Yeah, each dataLayer declaration would create a new dataLayer and we want to avoid that. What we need is a dataLayer.push and I couldn't get that working earlier.

    After a little reading I found a post saying dataLayer.push scripts need to after the Google Tag Manager container tag. Hopefully we've hit on a solution with this.

    Now, we open the head with

    %script{ type: "text/javascript"}
      dataLayer = [];

    And below the Google Tag Manager tag I added

    -if current_page.data.postType
      %script
        dataLayer.push({'postType': '#{current_page.data.postType}'});
    -if current_page.data.campaign
      %script
        dataLayer.push({'campaign': '#{current_page.data.campaign}'});
    -if current_page.data.tags
      %script
        dataLayer.push({'tags': #{current_page.data.tags.split(", ")}});
  • @shanerice That's great! Let me know what else I can do to help.

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment