We are developing a cloudpage using new content builder functionality with multiple HTML blocks (4-5 html blocks ) so my question is in which block we should keep our Google tag manager Java script code so we can track/see analytics of the entire cloud page and not that particular html block?
1 Answer
A couple things:
- You should add the tracking code via Code View as @StijnHoste said in the comment.
- Any Javascript inside of any content blocks will be removed once you reopen it.
- If you were to put the tracking inside a block, it would run for the whole page.
Code View
You are able to fully edit every aspect of your webpage 'template' inside of this view to allow you to change styles, scripts, meta tags, etc. This will display each content block placeholder as <div data-type="slot" data-key="xxxx">
though and will not display the actual content that is in the block here. This is where you would add your analytics tracking or any similar scripting.
Javascript in Content Block
As you can see in the official docs, JS is not allowed inside of content blocks. The way they say to handle it is to use 'code only' blocks (HTML or Code Snippet) and do not complete the script tags inside one block. Something like:
Block 1:
<script>
... my JS ...
Block 2:
</script>
I personally prefer a different method to host JS. See this post for the full answer. Basically you would either store your full JS script (including script tags) inside a DE field and then output inside a TreatAsContent or you would concat the script tags together via AMPscript so that the parser does not recognize the tags and does not strip them.
Lookup to DE:
%%[
SET @jscript = Lookup('yourFormDE', 'Javascript', 'Form', 'Form 1')
]%%
%%=TreatAsContent(@jscript)=%%
Concatting script blocks:
%%[
set @scriptBeg = Concat('<','script','>')
set @scriptEnd = Concat('</','script','>')
]%%
%%=v(@scriptBeg)=%%
your javascript code here
%%=v(@scriptEnd)=%%
Block affects all of page
This is just to note that the page blocks are compiled together into a single source prior to being sent from server to the client. This means that there is absolutely no difference between a page that uses blocks compared to a page that is raw HTML as seen by the client. Just wanted to make a note on this here as it seemed you were of the impression this was not true.