Forum Discussion

irach15's avatar
irach15
Maven
9 years ago

Google Tag Manager (GTM) dataLayer implementation help needed!

Did anyone implemented GTM dataLayer? We've added a dataLayer with custom parameters. I've done some search but was not able to find any additional info, except that is not much to pull from LITHIU...
  • irach15's avatar
    irach15
    9 years ago

    I put all code together for anyone who might struggle as I did :-)

    Some variables you can update to your naming, for example 'someRole'

    Put the code to your Studio hitbox,

    in my case also Google Tag Manager script has to be first before the code bellow

    .......

    <#assign userId = user.uid />
    <#assign languageSelected = restadmin("/users/self/profiles/name/language").value />
    
    <#if user.registered>
      <#assign someRole  = false />
      <#list restadmin("/users/id/${user.uid}/roles").roles.role as role>
        <#if role.name?? && (role.name == "someRole" || role.name == "Administrator")>
          <#assign someRole = true />
          <#break />
        </#if>
      </#list>
    </#if>
    
    <#if page.context.thread??>
      <#function dateFormat dt >
        <#return dt?string("MM-dd-yyyy, HH:mm Z") />
      </#function>
    
    <#assign apiVersion = "2.0"/>
    <#assign article = page.context.thread.topicMessage />
    <#assign articleId = page.context.thread.topicMessage.uniqueId?c />
    <#assign author = article.author />
    <#assign authorId = author.uid />
    <#assign primaryAuthor = author.login />
    
    <#assign contributorApi = rest("/messages/id/" + articleId + "/tkb/contributors").users.user.login />
    <#if contributorApi?size gt 0>
    <#assign contributor = "" />
      <#list contributorApi as con>
        <#assign contributor = contributor + con />
        <#if con_has_next>
          <#assign contributor = contributor + " | " />
        </#if>
      </#list>
    <#else>
        <#assign contributor = "0" />
    </#if>
    
    <#assign articlePostDate = dateFormat(article.postDate) />
    <#assign apiv2Article = rest("2.0", "/messages/" + articleId).data />
    
    <#assign articleUpdateDate = dateFormat(apiv2Article.current_revision.last_edit_time) />
    
    <#assign articleImageQuery = "SELECT id FROM images WHERE messages.id = '" + articleId + "'"/>
    <#assign apiv2articleImageCount = rest(apiVersion, "/search?q=" + articleImageQuery?url + "&restapi.response_style=view").data.items![] />
    <#if apiv2articleImageCount?size gt 0>
        <#assign articleImageCount = apiv2articleImageCount?size />
        <#else>
            <#assign articleImageCount = "0" />
    </#if>
    
    <#assign articleHelpfulQuery = "SELECT * FROM tkb_helpfulness_ratings WHERE message.id = '" + articleId + "'"/>
    <#assign apiv2articleHelpful = rest(apiVersion, "/search?q=" + articleHelpfulQuery?url + "&restapi.response_style=view").data.items![] />
    <#if apiv2articleHelpful?size gt 0>
    <#assign articleHelpful = apiv2articleHelpful?size />
    <#else>
    <#assign articleHelpful = "0" />
    </#if>
    
    <#assign labelsQuery = "SELECT * FROM labels WHERE messages.id = '" + articleId + "'"/>
    <#assign labelsApi = rest(apiVersion, "/search?q=" + labelsQuery?url + "&restapi.response_style=view").data.items![] />
    <#if labelsApi?size gt 0>
      <#assign labels = "" />
      <#list labelsApi as label>
        <#assign labels = labels + label.text />
        <#if label_has_next>
          <#assign labels = labels + " | " />
        </#if>
      </#list>
      <#else>
      <#assign labels = "0" />
    </#if>
    
    <#assign tagsQuery = "SELECT * FROM tags WHERE messages.id = '" + articleId + "'"/>
    <#assign tagsApi = rest(apiVersion, "/search?q=" + tagsQuery?url + "&restapi.response_style=view").data.items![] />
    <#if tagsApi?size gt 0>
      <#assign tags = "" />
      <#list tagsApi as tag>
        <#assign tags = tags + tag.text />
        <#if tag_has_next>
          <#assign tags = tags + " | " />
        </#if>
      </#list>
    <#else>
      <#assign tags = "0" />
    </#if>
    
    <#assign thumbStatus = rest("/messages/id/" + articleId + "/kudos/count").value />
    
    </#if>
    
    
    <!--  Google Analytics Data Layer  --->
    <script>
        if (typeof LITHIUM == 'undefined') 
            { var LITHIUM={}; };
        if (typeof LITHIUM.CommunityJsonObject == 'undefined') 
            { LITHIUM.CommunityJsonObject={}; };  
        if (typeof LITHIUM.CommunityJsonObject.Page.object.type == 'undefined') 
            { LITHIUM.CommunityJsonObject.Page.object.type == ''; };
        
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
        'event': 'dataLayer-loaded',
        'userLoggedIn': (!LITHIUM.CommunityJsonObject.User.isAnonymous),
        'region': '',
        'articleType': LITHIUM.CommunityJsonObject.Page.name,
        'pageType': LITHIUM.CommunityJsonObject.Page.object.type,
        'uniqueID': LITHIUM.CommunityJsonObject.User.id,
        'articleImageCount': '${articleImageCount!""}',
        'articlePostDate': '${articlePostDate!""}',
        'articleUpdateDate': '${articleUpdateDate!""}',
        'articleHelpful': '${articleHelpful!""}',
        'thumbStatus': '${thumbStatus!""}',
        'contributor': '${contributor!""}',
        'primaryAuthor': '${primaryAuthor!""}',
        'communityTeam': '${(someRole?c)!""}', 
        'labels': '${tags!""}', 
        'tags': '${labels!""}'
        });
    </script>