I have a site showing Amazon ads (Native Ads, the Search ones).
The ad code is placed directly in the body of the node.

There's a section in the code where a variable can be declared that can help to target the type of products that show in the ads. In the example below, I've set the variable to "books."

<script type="text/javascript">
amzn_assoc_default_search_phrase = "books";
</script>

I'm looking for a way to dynamically fill this variable on a node-by-node basis.
One idea is to place the node's Title in the variable since it will contain pertinent keywords (such as "mystery books").

I've installed the Token Filter module and can successfully print out in the node's body Tokens like [current-page:title].

But I can't figure out how to assign a Token as the value for my script variable. The following does not seem to work:

<script type="text/javascript">
amzn_assoc_default_search_phrase = "[current-page:title]";
</script>

And I think this form breaks the script altogether:

<script type="text/javascript">
amzn_assoc_default_search_phrase = [current-page:title];
</script>

Can someone steer me in the right direction to be able to accomplish this. Thanks.

Comments

Stefan Lehmann’s picture

Can you elaborate on "does not seem to work" please?

I like cookies!

Anonymous’s picture

I'm just going by what I see when I view the page.

When I hand code it as:

<script type="text/javascript">
amzn_assoc_default_search_phrase = "books";
</script>

I see ads about books.

When I code it as:

<script type="text/javascript">
amzn_assoc_default_search_phrase = "[current-page:title]";
</script>

No ads show, just an empty box.

When I code it as:

<script type="text/javascript">
amzn_assoc_default_search_phrase = [current-page:title];
</script>

The empty box shows but seems mis-formatted/broken.

Stefan Lehmann’s picture

So .. uhm .. does the token actually get replaced? Just have a look into the source code of your website...

Also do you understand the difference between PHP and JavaScript?

I like cookies!

rajeevk’s picture

1st question, is it Drupal 7 or 8?

Second, did you enable TOKEN for text format which you are using for content of the node?

Rajeev Kumar,
@drupler@bihar.social (ActivityPub)

Anonymous’s picture

I enabled "token for text format," I'm using Full HTML. The part of what I describe above that works wouldn't have worked if I hadn't done that.

Anonymous’s picture

In my initial posting it states 7.x

Anonymous’s picture

I'm under the impression that the problem here lies in the fact that I've placed the advertising code in a Block (that has been assigned for display in the footer of my Node).
Evidently by design node-specific Tokens aren't accessible to Blocks (because not all Blocks are attached to Nodes). So things like [node:title] or [current-page:title] have no value where as global tokens such as [site:name] do.

I'll mention that the Visibility Setting of a Block can be restricted to a Content Type. So it is possible to declare that a Block is always associated with a Node, and hence it would only make sense that the Block should be allowed all of the Node's Tokens. But that doesn't seem to be the way the Token system is designed.

Stefan Lehmann’s picture

Hm, yes .. node tokens don't work - which makes sense, as it is not guaranteed that a block will always be displayed in a node context.

But [current-page:title] should work, as it is always available regardless of the context. That's weird.

I like cookies!

Anonymous’s picture

After spending way too may hours on this, I've come up with three ways to solve my problem, which was basically getting a local token, like [node:title], into a block.

In my case, was trying to get a node's field token [node:field-my-variable] that's set on a per-node basis into a block so it could customize the output of the page's advertising javascript. The advantage of using the block format was that the advertisments could be placed outside of the node's body section (like below its comments section).

I installed the Token Filter module, so the token could be interpreted/used in the body of the block.

Method #1 - You could use the Custom Token module to create a global-type token (like [site:my_variable]). And then on a per-node basis assign a value to it (like the value stored in one of the node's fields). Custom global tokens definitely can be interpreted inside a block's body. But the coding needed to assign the global token a value (token_replace, or whatever) was beyond me so I wasn't able to complete this approach.

Method #2 - You can pass variables from nodes to blocks dynamically. Once again however, for me the level of coding needed was beyond my skills, so I didn't complete this approach.

Method #3 - Using the Field as Block module you can export a node's field into a block. And unexpected to me, that block can access the node's local tokens inside its body (so the blanket statement that local tokens can be used inside blocks isn't accurate).

This wasn't entirely the solution I wanted (I now have to include the advertising code in every node in one of it's fields), but it does get the job done.

I've just found as solution for what I've just mentioned. I've created a block that contains my advertising code, I've then assigned it to be the contents of one of my node's field (Block Reference module) (That does however have to be declared on a per-node basis). Then, just as above, you export the contents of the field as a block. Works fine.